不保留 PHP 会话变量 [英] PHP Session Variables Not Preserved
问题描述
我无法在设置它们的页面以外的页面上使用会话变量,它们就像非会话变量一样.我在其他六个类似的论坛上发现了一个类似的问题,但在其他情况下的答案总是不适用.
I am unable to use session variables on a page other than the one where they are set, IOW they act like non-session variables. I have found a similar question posted in half a dozen other similar fora, but the answer in those other cases always turns out not to apply.
这是我的文件:
sess1.php
<?php
session_start();
session_register("userid");
session_register("textvar");
$_SESSION['userid'] = 10333 ;
$_SESSION['textvar'] = TextVariable ;
echo "<p>User ID is: " . $_SESSION['userid'] . "</p>" ;
echo "<p>Another variable is: " . $_SESSION['textvar'] . "</p>" ;
?>
<p>Go to the <a href="sess2.php">next page</a>.</p>
还有,sess2.php
<?php
session_start();
echo "<p>The userid session variable is: " . $_SESSION['userid'] . "</p>";
echo "<p>The other session variable is: " . $_SESSION['newvar']. "</p> ";
?>
每种情况下的浏览器输出为:
The browser output in each case is:
sess1.php
用户 ID 为:10333
User ID is: 10333
另一个变量是:TextVariable
Another variable is: TextVariable
转到[下一页].
还有,sess2.php
userid 会话变量是:
The userid session variable is:
另一个会话变量是:
转到[最后一页].
有些事情不是:
- 我确实在两个文件的顶部都有 session_start().
- 变量目录是可写的,会话变量显示在那里.(我有大约一百个名为 sess_b62 的小文件,里面有这个:'userid|i:10333;textvar|s:12:"TextVariable";'.)
- phpinfo() 告诉我正在正确读取 php.ini 文件,并且生命周期设置为默认值 0,即直到浏览器关闭.
我的智商已经不行了.有什么建议吗?
I'm at my wit's end. Any suggestions?
非常感谢.
推荐答案
会话 ID 必须以某种方式携带,以便同一个会话可以在多个页面上使用.通常,这是通过 cookie 完成的(参见 session.use_cookies
),但也可以在 URL 或表单中完成(请参阅 session.use_trans_sid
).
The session ID has to be carried along in some way in order that the same session can be used over several pages. In general this is done with a cookie (see session.use_cookies
) but it can also be done in the URL or in forms (see session.use_trans_sid
).
因此首先您必须确保会话 ID 被传输,以便 PHP 可以加载正确的会话和会话数据.
So first you have to make sure that the session ID is transmitted so that PHP can load the right session and session data.
这篇关于不保留 PHP 会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!