PHP 会话变量不粘连
这是我的代码的简化版本
<?php session_start();
if($_GET['page'] == "login")
{ // process username/password
if(login is successful)
$_SESSION['access'] = "dev";
}
else if($_GET['page'] == "request")
{
//get some data from an xml file using simplexml
}
?>
<?php if(!isset($_SESSION['access']) { ?>
<!-- display login form. submits to this page + ?page=login -->
<?php } else { ?>
<!-- display edit form, nothing more than a textarea, a select and a submit button. -->
<!-- The form points to this page + ?page=request -->
<?php } ?>
发生的情况是,当我登录时 $_SESSION['access'] 设置正确,但是当我再次向页面提交数据以检索数据时,$_SESSION['access'] 被取消设置并且登录表单再次显示。
我做错了什么?我对 PHP 还很陌生。
Here is the simplified version of my code
<?php session_start();
if($_GET['page'] == "login")
{ // process username/password
if(login is successful)
$_SESSION['access'] = "dev";
}
else if($_GET['page'] == "request")
{
//get some data from an xml file using simplexml
}
?>
<?php if(!isset($_SESSION['access']) { ?>
<!-- display login form. submits to this page + ?page=login -->
<?php } else { ?>
<!-- display edit form, nothing more than a textarea, a select and a submit button. -->
<!-- The form points to this page + ?page=request -->
<?php } ?>
What happens is when I login $_SESSION['access'] is set correctly, but when I submit data again to the page to retrieve data the $_SESSION['access'] gets unset and the login form gets displayed again.
What am I doing wrong? I'm fairly new to PHP.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在会话中显式存储数据。 session_start 打开会话进行读/写,但写入的内容不会保留,除非您明确执行此操作。
http://www.php.net/manual/en/function .session-write-close.php
使用该函数。
You need to explicitly store the data in the session. session_start opens the session for reading/writing, but what is written does not stick until you explicitly do it.
http://www.php.net/manual/en/function.session-write-close.php
Use that function.