Jump to content

Recommended Posts

Posted

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/

Posted

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.

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...