PHP 会话和 session_start() [英] PHP sessions and session_start()
问题描述
抱歉,如果这是一个愚蠢的问题,但最近我一直在为客户设计一个网站,并且在我的会话中发生了一些奇怪的事情.
Sorry if this is a silly question but lately I've been designing a site for a client and something strange has been happening with my sessions.
该网站有一个 PayPal 按钮,可将用户重定向到 PayPal,以便他们在再次重定向到该网站之前确认付款.
The site has a PayPal button which redirects the user to PayPal so they can confirm a payment, before being redirected to the site again.
在重定向用户之前,会保存大量会话变量.其中一些与 PayPal 有关,其他与我网站上的内容有关,例如用于确定登录用户的变量、他们的购物车项目等.
Before the user is redirected, a load of session variables are saved. Some of them are to do with PayPal, others are to do with things on my site such as a variable to determine which user is logged in, their shopping cart items, etc.
现在,这里出现了问题......
Now, here's where things have been going wrong...
用户从 checkout.php 重定向到 PayPal.在它们被重定向之前,站点的所有会话变量都存在(由 var_dump 和 print_r 显示).这可以.
The user is redirected from checkout.php to PayPal. Before they're redirected, all session variables for the site are present (shown by var_dump and print_r). This is fine.
用户从 PayPal 返回到 orderreview.php,但 var_dump 和 print_r 现在显示站点会话变量丢失,但所有 PayPal 变量都存在.
The user returns from PayPal to orderreview.php, but var_dump and print_r now show that the site session variables are missing, but all PayPal ones are there.
我通过删除session_start();"解决了这个问题来自 orderreview.php 的顶部.
I fixed this problem by removing "session_start();" from the top of orderreview.php.
所以我的问题是,为什么删除该行可以解决问题?为什么以前不行?
So my question is, why did removing that line fix the issue? Why wouldn't it work before?
我以为我了解 PHP 会话,但显然我并不像我想象的那样了解它们.
I thought I understood PHP sessions but clearly I don't understand them as well as I thought.
我曾在某处读到过:
从 PHP 4.3.3 开始,在会话已经启动时调用 session_start() 将导致级别 E_NOTICE 的错误.此外,第二个会话启动将被忽略."
"As of PHP 4.3.3, calling session_start() while the session has already been started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored."
所以我假设如果会话已经提前启动,在脚本顶部调用 session_start 不会影响任何事情.
So I was under the assumption that calling session_start at the top of the script wouldn't affect anything if a session was already started earlier.
感谢您的回答,如果这是一个愚蠢的问题,我再次道歉.
Thanks for any answers, once again I apologise if this is a silly question.
推荐答案
那可能是因为您在此过程中被重定向到另一个站点.当您从 Paypal 返回到您的网站时,session_start()
生成了一个新的会话 ID,您之前存储的会话变量未链接到该 ID.
That's may be because that you have been redirected to another site during the process. And while you return from Paypal to your website, session_start()
generated a new session id which your previously stored session variables are not linked to.
当您删除 session_start()
时(我认为 session 应该在没有这个的情况下工作),它使用旧的 session id 并且从未重新生成.因此,旧的会话数据又回来了!
And when you removed session_start()
(I don't think session should work without this on top), it used the old session id and never got regenerated. Hence, old session data are back!
这只是我的假设.
这篇关于PHP 会话和 session_start()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!