PHP 文档已过期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10795552/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Document Expired
提问by user1383147
I doing some PHP coding, if the 'Back' button is pressed on the browser, I get the following error:
我在做一些 PHP 编码,如果在浏览器上按下“后退”按钮,我会收到以下错误:
Document Expired
This document is no longer available.
What code can I implement to cater to this situation
我可以实现哪些代码来满足这种情况
回答by T.Todua
Add this in the start of PHP codes:
在 PHP 代码的开头添加:
ini_set('session.cache_limiter','public');
session_cache_limiter(false);
回答by Sanjeev Chauhan
Set Cache-Control header in your main page.
在主页中设置 Cache-Control 标头。
<?php
header('Cache-Control: max-age=900');
?>
回答by Senthil Kumar Muppidathi
Using Post/Redirect/Get rule you can avoid this.
使用 Post/Redirect/Get 规则可以避免这种情况。
This problem will arise by the following:
出现这个问题的原因如下:
- Let say I have
example1.php,example2.phpandexample3.php - I am posting some values from
example1.phptoexample2.phpthen 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.phptoexample3.php. Now if you click browser back Document will Expire.
- 假设我有
example1.php,example2.php并且example3.php - 我发布了一些值
example1.php,example2.php然后我根据我的需要完成了所有数据库的工作并呈现了页面(未重定向 - 刚刚发布并呈现页面)。 - 之后,我将页面从 重定向
example2.php到example3.php。现在,如果您单击浏览器返回Document will Expire。
To Avoid this we can post the values from example1.phpto example2.phpand redirect the user to some other page immediately.
为了避免这种情况,我们可以将值从example1.phpto 发布example2.php并立即将用户重定向到其他页面。
This is Post/Redirect/Getpattern that can be followed to avoid document expire. It also helps avoid redundant entry in DB.
这是可以遵循的Post/Redirect/Get模式以避免文档过期。它还有助于避免数据库中的冗余条目。
回答by Pavan Patil
just put this line in your page.
只需将此行放在您的页面中即可。
<?php
header("Cache-Control: max-age=300, must-revalidate");
?>
回答by Kabir Hossain
Go your server's php.ini and change this
转到您服务器的 php.ini 并更改它
session.cache_limiter = nocache
As
作为
session.cache_limiter = public
The problem would be solved. I solved my problem with this.
问题就解决了。我用这个解决了我的问题。
回答by Gokul Shinde
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.
共有三个页面,page1.php、page2.php 和page3.php。现在我正在使用 POST 方法将一些表单从 page1.php 提交到 page2.php。从 page2.php 我点击了 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".
现在我想从 page3.php 回到 page2.php。但是 page2.php 有使用 POST 方法发布的表单值,当我从 page3.php 进入 page2.php 时,浏览器显示错误“Docuemnt Expired”。
So I used GETmethod insteadof 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.
所以我用GET方法代替的POST。所以当回到page2.php 时,表单的POST 值将不会出现,页面将正确加载。此外,由于 URL 中存在表单值,因此页面将按预期加载。
回答by zapping
Check if caching is disabled on the header like
检查是否在标头上禁用了缓存,例如
<HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
<HTTP-EQUIV="PRAGMA" CONTENT="NO-CACHE">
回答by Bsienn
This problem will arise by the following two scenarios:
以下两种情况会出现这个问题:
- Implementing searching with
Post - Redirecting back to a page that was posted previously.
- 实现搜索
Post - 重定向回之前发布的页面。
There are 2 ways to overcome this issue easily without any hack.
有 2 种方法可以轻松解决此问题而无需任何黑客攻击。
For search formdo not use postmethod, instead use get method and everything works fine.
因为search form不要使用post方法,而是使用 get 方法,一切正常。
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.
如果您出于某种原因确实需要隐藏表单输入并希望使用 post 方法,那么导致重定向到其他页面的链接/操作,使其通过 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.
这将删除引用 URL 并强制执行新的 http 请求。现在按浏览器上的后退按钮不会导致文档过期。

