Kaula Posted May 25, 2006 Posted May 25, 2006 Hi, I'm having some trouble using a javascript lastModified script when the pages file extension is .php 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. Note, I added seconds so you can see the last updated changing everytime you refresh or someone views the page. The javascript : http://www.kaulaiscool.com/tch_example/tch_example.txt It working fine without the .php extensions: http://www.kaulaiscool.com/tch_example/tch_example.htm The problem... http://www.kaulaiscool.com/tch_example/tch_example.php You can see if you refresh the last link with the .php extension that the seconds gets updated everytime.. 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) Thanks for any help you can give! Paul http://www.kaulaiscool.com/tch_example/ Quote
Kaula Posted May 25, 2006 Author Posted May 25, 2006 Some things I have tried that didnt work; <?php echo 'javascript contents' ?> <?php ?> javascript contents <?php ?> Quote
Deverill Posted May 25, 2006 Posted May 25, 2006 It is possible that using PHP to modify the file returned to the browser may be changing the lastmodified time to now. You may try to read the time that the file itself was last modified with filemtime. Quote
TweezerMan Posted May 25, 2006 Posted May 25, 2006 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;'. 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. 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. 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): ><?php $mtime=filemtime($_SERVER["SCRIPT_FILENAME"])-date("Z"); $gmt_mtime = date('D, d M Y H:i:s', $mtime) . ' GMT'; header("Last-Modified: ".$gmt_mtime); ?> With the above code in place, the time does not continually update every time you refresh the page. Quote
Kaula Posted May 25, 2006 Author Posted May 25, 2006 Howdy, Thanks for the replies, they worked perfectly! Looks like I have alot of learning to do! Thanks, Paul Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.