Deep Tech Fusion

tech related weblog

Update Word document through PHP

$filename = “C:\\document_”.date(“YmdHis”).”.doc”;

$template = “C:\\template.htm”;

$fp_temp = fopen($template, “rb”);
$fp = fopen($filename, “wb”);

$old_contents = fread ( $fp_temp, filesize ($template));
fclose ( $fp_temp);
$new_constants = str_replace(“var_username”, “Deepak”, $old_contents);

$rset = “<table width=’100%’ ><tr><td>#</td><td >Name</td><td >Date of Join</td><td >Title</td><td >Dept</td></tr>”;
$rset .= “<tr><td>123</td><td>Deepak</td><td>Oct 10, 2003</td><td>Programmer</td><td>IT</td></tr>”;
$rset .= “<tr><td>123</td><td>Sachin</td><td>Oct 10, 2005</td><td>Developer</td><td>IT</td></tr>”;
$rset .= “<tr><td>123</td><td>Amita</td><td>Oct 10, 2006</td><td>System Admin</td><td>HR & Admin</td></tr>”;
$rset .= “<tr><td>123</td><td>Deepa</td><td>Oct 10, 2002</td><td>Hr Head</td><td>HR & Admin</td></tr>”;
$rset .= “<tr><td>123</td><td>Poja</td><td>Oct 10, 2003</td><td>Developer</td><td>IT</td></tr></table>”;
$constants = str_replace(“Var_record_sets”, $rset, $new_constants);

fwrite($fp, $constants) ;

Advertisements

December 28, 2006 Posted by | PHP | Leave a comment

Ajax Cache-Control

Introduction
A ”’Web cache”’ sits between one or more Web servers (also known as origin servers) and a client or many clients, and watches requests come by, saving copies of the responses — like HTML pages, images and files (collectively known as representations) — for itself.
Then, if there is another request for the same URL, it can use the response that it has, instead of asking the origin server for it again.

When developing with Ajax-based application we need to disable Cache.

This can be done by various different way….

Some of them are discussed here…..

Without Using Headers

* Ajax – Cache Busting with Javascript — [fool the browser]

Browsers are notorious at hanging on tenaciously to cached pages. Even when you use all manner of fancy headers like ””Pragma: no-cache” or “Cache-Control: must-revalidate”” you’ll often find that you receive a cached page rather than a ‘live’ one. Here’s something else you can try:

To the URL which requests the page, ”’add a random (and meaningless) variable and value pair”’. The value should be different each time, and although it will have no effect on the called page, it helps to fool the browser into thinking it has to return a page from a URL it’s never visited before, rather than realising it has it in cache.
A good way to do this in Javascript is to add a random number:
var myDate = new Date();
var myTime = myDate.getTime();
var url=’http://localhost/call_to_ajax.php?rand=’+myTime;
http.open(‘GET’, url);

Now, each time we call the page whose address is stored in url, we append a meaningless variable rand with a random value,

eg: http://localhost/call_to_ajax.php?rand=321;

* Http: A Caching XmlHttpRequest Wrapper

A wrapper for XmlHttpRequest that supports forced caching on FireFox and forced non-caching on IE.
XmlHttpRequest is a class available in client-side JavaScript for making new HTTP requests from within a page. It is implemented as a built-in object in Safari and FireFox, and as an ActiveX object on Internet Explorer.
More about wrapper class is at http://www.adamv.com/dev/javascript/http_request&#8217;

Download this wrapper from http://www.adamv.com/dev/javascript/files/request.js

Using Headers

* PHP – Files

we need to add the following line into the files which will call by open method of object createRequestObject class.

e.g.
http.open(‘GET’, ‘call_to_ajax.php’);
so we need to add following line into the ”’call_to_ajax.php”’ file.

header(‘Expires: Fri, 09 Jan 1981 05:00:00 GMT’);
header(‘Cache-Control: no-store, no-cache, must-revalidate’);
header(‘Cache-Control: post-check=0, pre-check=0’, FALSE);
header(‘Pragma: no-cache’);

* .htaccess – Files

First enables expires and headers module of apache [httpd.conf]
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so

Then add the following lines into .htaccess file
### activate mod_expires
ExpiresActive On
### Expire everything else 1 day before
ExpiresDefault “modification M-3600”
### Apply a Cache-Control header to call_to_ajax.php

Header append Cache-Control “public, must-revalidate”

* PERL – Files

Add the following lines of call_to_ajax.cgi or call_to_ajax.pl file
#!/usr/bin/perl
print “Content-type: text/html\n”;
print “Expires: Thu, 29 Oct 1998 17:04:19 GMT\n”;
print “\n”;

References and Further Information
Caching Tutorial
http://www.mnot.net/cache_docs/
Brian D. Davison’s Web Caching and Content Delivery Resources
http://www.web-caching.com/
Hypertext Transfer Protocol — HTTP/1.1
http://www.ietf.org/rfc/rfc2616.txt

December 5, 2006 Posted by | AJAX, APACHE, JAVASCRIPT, PERL, PHP | 1 Comment