如何跨页面使用store和session变量?
当访问一个页面时,我想启动一个会话并存储一个会话变量:
<?php
session_start();
$_SESSION['myvar']='myvalue';
?>
然后从另一个页面,我想检查该会话变量是否已存储:
<?php
session_start();
echo("1");
if(isset($_SESSION['myvar']))
{
echo("2");
if($_SESSION['myvar'] == 'myvalue')
{
echo("3");
exit;
}
}
?>
此代码对我不起作用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
逐步进行会话
在一切之前定义会话,在此之前不应有任何输出,无输出
<前><代码>
在页面内设置会话,然后您就可以访问该页面。例如,这是第 1.php 页
<前><代码>[电子邮件受保护]';
?>
在2.php中使用和获取会话
<前><代码>
注意:注释没有输出。< /em>
Sessions Step By Step
Defining session before everything, No output should be before that, NO OUTPUT
Set your session inside a page and then you have access in that page. For example this is page 1.php
Using and Getting session in 2.php
NOTE: Comments don't have output.
你要做的就是写 --- session_start(); ----- 在两页上..
All you want to do is write --- session_start(); ----- on both pages..
从对此问题的评论进行推理,似乎缺乏调整后的 session.save_path 会导致 PHP 会话处理程序出现这种错误行为。只需指定一个存在且可由 PHP 读取和写入的目录(在文档根目录之外)即可解决此问题。
Reasoning from the comments to this question, it appears a lack of an adjusted session.save_path causes this misbehavior of PHP’s session handler. Just specify a directory (outside your document root directory) that exists and is both readable and writeable by PHP to fix this.
如果第二个页面没有对会话 cookie 的共享访问权限,您需要使用 session_set_cookie_params:
并且
In the possibility that the second page doesn't have shared access to the session cookie, you'll need to set the session cookie path using session_set_cookie_params:
And
试试这个
第一页
第二页
Try this
First Page
Second page
每次启动会话(适用于 PHP 版本 5.2.54)时,
session_start()
都会创建一个新的会话 ID。这是对我有用的修复。
文件1.php
文件2.php
Every time you start a session (applies to PHP version 5.2.54),
session_start()
creates a new session id.Here is the fix that worked for me.
File1.php
File2.php
启动会话:
将以下代码放在文件顶部。
存储会话变量:
检查会话变量中是否存储了数据:
?>
详细信息请参见http://skillrow.com/sessions-in-php-4/
Starting a Session:
Put below code at the top of file.
Storing a session variable:
To Check if data stored in session variable:
?>
detail here http://skillrow.com/sessions-in-php-4/
试试这个:
Try this: