简体   繁体   English

在PHP中注销时重定向到旧页面

[英]Redirect to old page at logout in PHP

I am developing a CMS. 我正在开发CMS。 I have three scripts, and 2 session variables 我有三个脚本和两个会话变量

 $_SESSION['logged']
 $_SESSION['redirect'] 

"auth.inc.php" - Run every time a user requests a page. “ auth.inc.php”-每次用户请求页面时运行。 It checks if $_SESSION['logged'] is set and equal to 1. If it isn't redirects ti main page using 它检查$ _SESSION ['logged']是否已设置并等于1。如果不是,则使用

header(location:index.php)

and the page referrer link is saved in $_SESSION['redirect']. 并且页面引荐来源网址链接保存在$ _SESSION ['redirect']中。 If $_SESSION['logged'] is set inactivity time is less than a required value it redirects to the requested page other wise it redirects to logout.php 如果将$ _SESSION ['logged']设置为非活动时间小于所需的值,它将重定向到请求的页面,否则将重定向到logout.php

"index.php" - checks for user name and password. “ index.php”-检查用户名和密码。 I successful sets 我成功套

$_SESSION['logged']=1

If $_SESSION['redirect'] is set it redirects to that page otherwise to deafult page 如果设置了$ _SESSION ['redirect'],它将重定向到该页面,否则重定向到默认页面

"logout.php" - Its unsets and destroys the session using “ logout.php”-使用以下命令取消设置并破坏会话

session_unset();
session_destroy();

But I want if user is logged out due to session timeout then after login it should be redirected to old page. 但是我想如果用户由于会话超时而注销,那么登录后应该将其重定向到旧页面。 Since I was unsetting the session $_SESSION['redirect'] was lost. 由于我取消了会话$ _SESSION ['redirect']的操作,因此丢失了。 So instead of using session_unset() I only unset $_SESSION['logged'] 因此,我只设置了$ _SESSION ['logged']而不使用了session_unset()

unset($_SESSION['logged']);
session_destroy();

but still I am not able to retrieve $_SESSION['redirect'] after session_destroy(). 但仍然无法在session_destroy()之后检索$ _SESSION ['redirect']。 The variable is still not set. 该变量仍未设置。 How should I redirect to old page after logout due to session expire 由于会话过期而注销后如何重定向到旧页面

session_destroy() will, in fact, destroy the data for the current session. session_destroy()实际上将破坏当前会话的数据。 To use any value you set, you have to session_start() again. 要使用您设置的任何值,您必须再次使用session_start()。

Furthermore, if the session expires, nothing you have set as a session cookie will be valid. 此外,如果会话期满,则您设置为会话Cookie的任何内容均无效。

The best thing you could do is to save your last page or whatever in the database, since you said the users can log in. Then fetch that value upon login, and redirect to it. 最好的办法是将最后一页或任何内容保存在数据库中,因为您说过用户可以登录。然后在登录时获取该值,然后重定向到该值。

Good luck! 祝好运!

Well your first issue is that you are still destroying $_SESSION['redirect'] when you call session_destroy(). 好吧,您的第一个问题是,当您调用session_destroy()时,您仍在销毁$ _SESSION ['redirect']。 What you could do is either hard code a default URL into the system so that you can destroy the session and still redirect. 您可以做的是将一个默认URL硬编码到系统中,以便可以破坏会话并仍然重定向。 The other option is to remove the session_destroy() command until after you have redirected.... ie on the redirect page, check to see if the previous page (referer) was logout.php, if it was destroy the session otherwise dont 另一个选项是删除session_destroy()命令,直到您重定向之后为止。...即,在重定向页面上,检查上一页(引用者)是否为logout.php,如果该页面被销毁了,否则不要

@Marc Towler: Sorry, I can't post comment now. @Marc Towler:对不起,我现在不能发表评论。 What I want to say is that not all the Browser support referer page, like IE. 我要说的是,并非像IE一样,所有浏览器都支持引用页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM