[英]PHP Session variables not preserved across pages
I'm new to PHP and I'm facing some problems when working with PHP sessions 我是PHP新手,在使用PHP会话时遇到一些问题
Let's say I have a file ( index2.php
) with this code in it. 假设我有一个文件( index2.php
),其中包含此代码。
<?php
session_start();
$_SESSION['name'] = 'The User';
?>
<a href="index3.php">Click</a>
And this is index3.php
这是index3.php
<html>
<head>
</head>
<body>
<h1>
<?php
echo $_SESSION['name'];
?>
</h1>
</body>
</html>
For some reason I don't understand, index3.php
doesn't show anything. 由于某种原因,我不明白, index3.php
没有显示任何内容。 What am I doing wrong? 我究竟做错了什么?
Thanks! 谢谢!
In index3.php
you need to start the session as well. 在index3.php
您还需要启动会话。 As per the official PHP docs : 根据官方的PHP文档 :
When session_start() is called or when a session auto starts, PHP will call the open and read session save handlers. 当调用session_start()或自动启动会话时,PHP将调用打开并读取会话保存处理程序。
Using your example, just initiate session_start()
as follows: 使用您的示例,只需启动session_start()
,如下所示:
<?php
session_start();
?>
<html>
<head>
</head>
<body>
<h1>
<?php
echo $_SESSION['name'];
?>
</h1>
</body>
</html>
Make sure you also have session_start(); 确保您还有session_start(); in all php pages where you want to retain and work with sessions; 在所有您要保留并使用会话的php页面中;
make sure index3.php contains session_start(); 确保index3.php包含session_start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.