Document.lastmodified With Php Problems

windows

Guest
Hi,<br /><br />I'm having some trouble using a javascript lastModified script when the pages file extension is .php<br />The page needs to stay with an extension of .php and use javascript for the lastModified, theres no way around that for what I need. <br /><br />Note, I added seconds so you can see the last updated changing everytime you refresh or someone views the page.<br /><br /><br />The javascript :<br /><a href="http://www.kaulaiscool.com/tch_example/tch_example.txt" target="_blank">http://www.kaulaiscool.com/tch_example/tch_example.txt</a><br /><br />It working fine without the .php extensions:<br /><a href="http://www.kaulaiscool.com/tch_example/tch_example.htm" target="_blank">http://www.kaulaiscool.com/tch_example/tch_example.htm</a><br /><br />The problem...<br /><a href="http://www.kaulaiscool.com/tch_example/tch_example.php" target="_blank">http://www.kaulaiscool.com/tch_example/tch_example.php</a><br /><br /><br />You can see if you refresh the last link with the .php extension that the seconds gets updated everytime..<br />I'm looking for a way around it or a reason why its impossible ( I'm new to scripting so I might be missing something obvious)<br /><br />Thanks for any help you can give!<br /><br />Paul<br /><br /><a href="http://www.kaulaiscool.com/tch_example/" target="_blank">http://www.kaulaiscool.com/tch_example/</a><!--content-->
Some things I have tried that didnt work;<br /><br /><?php echo 'javascript contents' ?><br /><br /><?php<br />?><br />javascript contents<br /><?php<br />?><!--content-->
It is possible that using PHP to modify the file returned to the browser may be changing the lastmodified time to now.<br /><br />You may try to read the time that the file itself was last modified with <a href="http://www.zend.com/manual/function.filemtime.php" target="_blank">filemtime</a>.<!--content-->
When the page does not have a .php extension (like your .htm page example), the web server itself sends a "Last-Modified" header with the page. This is what your javascript reads when it calls 'document.lastModified;'.<br /><br />When the page has a .php extension, the server does not automatically send a "Last-Modified" header with the page. Since PHP is a dynamic language, the web server has no way to know on its own what the "Last-Modified" date of the page is, or indeed even if the page has changed at all.<br /><br />Since no "Last-Modified" header being sent when the page is PHP, it would appear that the "document.lastModified;" javascript is defaulting to "now", and this would explain why your date/time is changing every time the page is refreshed.<br /><br />To make the javascript work as you intend, you need to send your own "Last-Modified" header along with your page. One way to do this is to add the following code at the very beginning of your page (before even the "DOCTYPE" tag):<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1--><?php<br />$mtime=filemtime($_SERVER["SCRIPT_FILENAME"])-date("Z");<br />$gmt_mtime = date('D, d M Y H:i:s', $mtime) . ' GMT';<br />header("Last-Modified: ".$gmt_mtime);<br />?><!--c2--></div><!--ec2--><br />With the above code in place, the time does not continually update every time you refresh the page.<!--content-->
Howdy,<br /><br /><br />Thanks for the replies, they worked perfectly!<br />Looks like I have alot of learning to do!<br /><br /><br />Thanks,<br />Paul<!--content-->
 
Back
Top