user1383147
user1383147

Reputation: 1083

PHP Document Expired

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

Upvotes: 24

Views: 38701

Answers (8)

Pavan Patil
Pavan Patil

Reputation: 61

just put this line in your page.

<?php 
header("Cache-Control: max-age=300, must-revalidate"); 
?>

Upvotes: 6

Abdul Rehman
Abdul Rehman

Reputation: 1794

This problem will arise by the following two scenarios:

  • Implementing searching with Post
  • Redirecting back to a page that was posted previously.

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.

Upvotes: 0

Using Post/Redirect/Get rule you can avoid this.

This problem will arise by the following:

  • Let say I have example1.php, example2.php and example3.php
  • I am posting some values from 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).
  • After that I have redirected the page from 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.

Upvotes: 17

Kabir Hossain
Kabir Hossain

Reputation: 3105

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.

Upvotes: 2

T.Todua
T.Todua

Reputation: 56487

Add this in the start of PHP codes:

ini_set('session.cache_limiter','public');
session_cache_limiter(false);

Upvotes: 27

Gokul Shinde
Gokul Shinde

Reputation: 965

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.

Upvotes: 1

Sanjeev Chauhan
Sanjeev Chauhan

Reputation: 4097

Set Cache-Control header in your main page.

<?php
header('Cache-Control: max-age=900');
?>

Upvotes: 19

zapping
zapping

Reputation: 4116

Check if caching is disabled on the header like

<HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">

Upvotes: 1

Related Questions