I doing some PHP coding, if the 'Back' button is pressed on the browser, I get the following error:
Document Expired
This document is no longer available.
What code can I implement to cater to this situation
I doing some PHP coding, if the 'Back' button is pressed on the browser, I get the following error:
Document Expired
This document is no longer available.
What code can I implement to cater to this situation
Add this in the start of PHP codes:
ini_set('session.cache_limiter','public');
session_cache_limiter(false);
Set Cache-Control header in your main page.
<?php
header('Cache-Control: max-age=900');
?>
Using Post/Redirect/Get rule you can avoid this.
This problem will arise by the following:
example1.php
, example2.php
and example3.php
example1.php
to example2.php
then I did all the DB stuff as per my need and rendered the page (Not Redirected - Just posted and the page got rendered).example2.php
to example3.php
.
Now if you click browser back Document will Expire.To Avoid this we can post the values from example1.php
to example2.php
and redirect the user to some other page immediately.
This is Post/Redirect/Get pattern that can be followed to avoid document expire. It also helps avoid redundant entry in DB.
Go your server's php.ini and change this
session.cache_limiter = nocache
As
session.cache_limiter = public
The problem would be solved. I solved my problem with this.
Check if caching is disabled on the header like
<HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
I have gone through same problem. A page where I want to come back had posted values of form and so when I hit Back link, it was showing Document Expired error. See example-
There are three pages, page1.php, page2.php and page3.php. Now I am submitting some form from page1.php to page2.php using POST method. From page2.php I clicked some link of page3.php.
Now I want to come back on page2.php from page3.php. But page2.php have form values posted using POST method and when I come on page2.php from page3.php, browser showing error "Docuemnt Expired".
So I used GET method instead of POST. So when come back on page2.php, then there will not be POST values of form and page will load properly. Also, as form values are present in URL, it page will load as expected.
just put this line in your page.
<?php
header("Cache-Control: max-age=300, must-revalidate");
?>
This problem will arise by the following two scenarios:
Post
There are 2 ways to overcome this issue easily without any hack.
For search form
do not use post
method, instead use get method and everything works fine.
If you really need to hide the form inputs for whatever reason and want to use post method, then the link/action that causes the redirect to other page, make it redirect through JavaScript.
location.replace('http://example.com/page2');
This removes the referral URL and force a new http request. Now pressing back button on browser wont cause the document expire.