如何使用PHP从一个页面继续到另一个页面的会话 [英] How do I continue a session from one page to another with PHP
问题描述
好的,我设置了一个会话...但是现在如何在其他页面上使用它呢?
我尝试做
@session_start();
if(isset($_SESSION['$userName'])) {
echo "Your session is running " . $_SESSION['$userName'];
}
如果您的PHP设置是明确的(会话写正常)并且cookie通常发送到浏览器(并保留了),则您应该可以执行这样的操作>
在第一页上:
session_start();
$_SESSION['userName'] = 'Root';
在第二页上:
session_start();
if(isset($_SESSION['userName'])) {
echo "Your session is running " . $_SESSION['userName'];
}
请注意,在发送任何输出之前,必须先调用session_start(),因此,如果必须在会话_start中使用@,则会隐藏警告.
由于这些是警告,如果给定的示例不起作用,请尝试在调用session_start之前添加它:
error_reporting(E_ALL);
Ok I set up a session... but now how do I make it work on my other pages?
I tried doing
@session_start();
if(isset($_SESSION['$userName'])) {
echo "Your session is running " . $_SESSION['$userName'];
}
If your PHP setup is clear (session writing ok) and cookie normally sent to browser (and preserved), you should be able to do something like this
On first page :
session_start();
$_SESSION['userName'] = 'Root';
On a second page :
session_start();
if(isset($_SESSION['userName'])) {
echo "Your session is running " . $_SESSION['userName'];
}
Be careful session_start() must be called before any output is sent, so if you had to use the @ for session_start it can hide warnings.
As these are warnings, if given example doesn't work try to add this before calling session_start :
error_reporting(E_ALL);
这篇关于如何使用PHP从一个页面继续到另一个页面的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!