PDA

View Full Version : Incorrect Time Displays When Back Button Is Used



R749er
04-02-2008, 08:25 AM
1) Script Title: Local Time Script

2) Script URL (on DD): http://dynamicdrive.com/dynamicindex6/localtime.htm

3) Describe problem: I have recently been using the DD Local Time Script on my Web site with no problem. However, a couple of days ago, a visitor to my site told me that the clock was off by five minutes. I did not understand how that was possible since the Local Time Script gets the time from the server. After checking, I realized that whenever I navigated to another page and then clicked the back button, the clock would start up from where it left off when I last visited that page. It does not correct itself unless and until I click the refresh button.

At first, I thought the problem was strictly with my Web site, but I checked another site that uses the Local Time Script, and the same thing occurred there. I even checked to see if it would happen at DD's Local Time Script page, and it did. When I clicked to another page, waited, and then clicked the back button, the time was off there, too.

Is there any way to correct this problem? I don't want to assume that all visitors to my Web site will know that if they use the back button to navigate around the site, they will also have to click refresh if they want to see the correct time.

-Thanks

codeexploiter
04-02-2008, 08:57 AM
I think whenever you click on the back button of the browser window the time display page displays the page from the last state where you left it. It seems to me that the problem is regarding the browser cache.

You can try to avoid this problem using the following meta tag by inserting this one in your time display page and check whether the problem exists or not


<meta http-equiv="cache-control" content="no-cache">

Or if you are using PHP or any other server-side tool you can try to avoid the usage of cache using those tools directly.

ddadmin
04-02-2008, 09:36 AM
codeexploiter is right. Basically the problem isn't so much the script but the fact that browsers cache the contents of a page, including the current time generated by the server and added to the script. You'll have to choose one of the ways that's available to prevent page caching if this is really an issue for you.

R749er
04-02-2008, 07:10 PM
Or if you are using PHP or any other server-side tool you can try to avoid the usage of cache using those tools directly.

Thanks, codeexploiter. I do use php on my Web site, but I am very new to it. Is there a php code that I should input in order to avoid the usage of cache so that my clock gives the correct time even when the back button is used?

-Thanks

ddadmin
04-02-2008, 11:51 PM
It doesn't matter if your pages are PHP. You can still add the mentioned META tag to the top of your generated page source, inside the HEAD section:


<meta http-equiv="cache-control" content="no-cache" />

codeexploiter
04-03-2008, 03:04 AM
Yes ddadmin is right you can achieve no cache using the <meta> tag like the way I've mentioned in my previous post.

You can check the following URL in case of a PHP based solution and check the Example 2 in the page for more details

http://in.php.net/header

R749er
04-03-2008, 06:56 AM
You can check the following URL in case of a PHP based solution and check the Example 2 in the page for more details

http://in.php.net/header

Thanks for your help, ddadmin and codeexploiter. I tried the meta tag, but it didn't work for me. However, I did check out the URL you suggested, codeexploiter, and it led me to the following solution, which worked like a charm:

<?php
header('Expires: Mon, 26 Jul 1997 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');
?>

Thanks again you guys!