我已经创建了一个登录表单。它工作正常,但如果用户在注销后单击后退按钮,它会返回到登录页面。 当我尝试阻止它时,索引页面停止工作。它显示加载页面错误的问题。
我在索引页面中执行了此操作,以避免用户不返回登录 session ,但现在索引页面本身不起作用。
if (!isset($_SESSION['user'])) {
header('Location:index.php');
}
注销.php:
session_start();#This will start the session
session_unset(); #Session_unset and Session_destroy
session_destroy();#Will remove all sessions.
header("location:index.php");#This code will sen du back to the index page
用户登录后,即登录到此主页。我添加了这样的 session :
session_start();
$user = $_SESSION['user'];
有不对的地方请指正。我面临的问题是:
- 显示我的索引页
- 避免用户在注销后返回登录 session
最佳答案
Follow the Post/Redirect/Get pattern.
Post/Redirect/Get (PRG) is a common design pattern for web developers to help avoid certain duplicate form submissions and allow user agents to behave more intuitively with bookmarks and the refresh button.
When a web form is submitted to a server through an HTTP POST request, a web user that attempts to refresh the server response in certain user agents can cause the contents of the original HTTP POST request to be resubmitted, possibly causing undesired results, such as a duplicate web purchase.
有用的读物:
<小时/>您可以做的最简单的事情就是在 header.php(或共享的类似文件)中放置一个条件:
session_start(); // on top of the scrit
................
...............
if (! isset($_SESSION['yourKey'])){
header('LOCATION: notLoggedPage.php');
exit;
}
注意:只要脚本中有与 session 相关的函数,请务必将 session_start()
放在脚本的开头。
关于php - 即使用户通过单击“后退”注销后,如何避免进入登录 session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8354787/