简体   繁体   English

PHP无法识别现有会话,因此开始新的会话

[英]PHP does not recognize existing session, starts new one

I have a website with a php log in. Everytime a page is visited, I call on session_start() . 我有一个使用php登录的网站。每次访问页面时,我都会调用session_start()

I've been having this unusual problem where 1 in 5 times (or so), my session_start will fail to recognize the existing PHPSESSID cookie and instead creates a new one, so that I have 2 of them, and stops using all of the data stored in the first session. 我一直遇到这个异常问题,其中PHPSESSID ),我的session_start将无法识别现有的PHPSESSID cookie,而是创建了一个新的cookie,因此我有两个,并停止使用所有数据存储在第一个会话中。

It goes as such: I log in successfully, and am successfully redirected to the same page I was on but now logged in, and I am assigned a session id cookie, and the session successfully stores its data (all checked and confirmed) 它是这样的:我成功登录,并成功重定向到与我之前登录但现在登录的页面相同,并为我分配了会话ID Cookie,并且该会话成功存储了其数据(均已检查并确认)

Then I click any link on the page (even the link that takes me to the same page I'm on) and instead of continuing the session, it creates a new session, giving me a second session cookie and effectively logging me out. 然后,我单击页面上的任何链接(甚至是将我带到所在页面的链接),而不是继续会话,它创建了一个新会话,为我提供了第二个会话cookie并有效地注销了我。

This only happens 1 in 5 times, and only when I first visit the website. 这种情况只有五分之一,而且只有在我第一次访问该网站时才会发生。 On successful trials, when I log in, navigating to another page leaves me with 1 session id cookie instead of 2. 在成功的试用中,当我登录时,导航到另一个页面后,我得到的是一个会话ID Cookie,而不是2。

I've been banging my head on the wall with this problem, and would love any help! 我一直在为这个问题而烦恼,很乐意提供任何帮助!

Call: echo session_id(); 呼叫: echo session_id(); . What is returned? 返回什么?

This is your session ID. 这是您的会话ID。 If it changes during the flow of your script, you will lose your $_SESSION data. 如果在脚本执行过程中发生更改,则将丢失$_SESSION数据。

You may be destroying and recreating the session. 您可能正在破坏并重新创建会话。 Make sure that the session doesn't exist before you create it. 在创建会话之前,请确保该会话不存在。

if(!$_SESSION)
   session_start();

obviously you will have sorted this, but for others who struggle with the same thing, i used this method in my logout.php file. 显然,您会对此进行排序,但是对于其他遇到相同问题的人,我在logout.php文件中使用了此方法。 the session_regenerate_id() resets the session variable used. session_regenerate_id()重置使用的会话变量。 hope this helps others 希望这对别人有帮助

<?php   
session_start(); //to ensure you are using same session
session_unset($_SESSION['currentUser']);
session_destroy(); //destroy the session
$_SESSION = array();
session_regenerate_id(TRUE);
header("location:/final/index.html"); //to redirect back to "index.php" after logging out
exit();
?> 

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

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