0

页面下方的代码在 GET 请求或刷新浏览器时保持会话,但是当我提交表单时,会话数据会丢失。

$user=$_POST['user']; $pass=$_POST['pass'];
if ($_POST['user'])
{ if($user==$un and $pass=$pw)
  { $_SESSION['uid']=$Xid;header('Location: '.$uri.'?welcome'); }
  else { $msg="chybny login";  }
}

if(isset($_GET['logout']))   { session_destroy(); header('Location: '.$uri); }
$cnt=$_SESSION['cnt']+1; $_SESSION['cnt']=$cnt;

上面是登录代码,它在验证后将我重定向到欢迎页面,但是会话丢失了。如果我只是刷新或重复加载页面而不提交,则会话通过回显会话变量 cnt(计数为 1、2、3、...)来保持

提交表单后,我看到会话丢失并且 cnt 变量也被重置?

4

5 回答 5

0

I usually don't work with session directly try the following, place it a the top of your script :

session_start();
$uid = $_SESSION['uid'];
$cnt = $_SESSION['cnt'];

then work with the variable instead

于 2013-02-19T22:08:44.503 回答
0

问题可能是您的“和”陈述。它应该是 &&。条件不会成立。

于 2013-02-19T22:11:35.053 回答
0

PHP.ini根据您上面的评论,如果您 100% 确定代码一切正常并且问题就是问题所在。查看此链接以检查.ini http://php.net/manual/en/session.configuration.php中的设置

于 2013-02-19T22:18:55.440 回答
0

首先,确保你在每个页面上做的第一件事是启动一个会话(我建议在你所有子站点上需要的头文件中调用它一次)。

这样你就有 session_start(); 系统中的任何地方。

其次,收紧你的代码;使其更易于阅读。就像是

$userName = isset($_POST['userName']) ? $_POST['userName'] : false;
$password = isset($_POST['password']) ? $_POST['password'] : false;
$logout = isset($_POST['logout']) ? $_POST['logout'] : false;

$url = '../index.php';

if(!($logout))
{
    if($userName && $password)
    {
        if($userName == $un && $password == $pw)
        {
            $_SESSION['loggedIn']=true;
            $_SESSION['uid']=$Xid;
            $_SESSION['message']="success";
        }
        else
        {
            $_SESSION['loggedIn']=false;
            $_SESSION['message']="fail, incorrect login information.";
        }
    }
    else
    {
        $_SESSION['loggedIn']=false;
        $_SESSION['message']="fail ; username and password not submitted.";
    }
    header("Location: $url");

}
else
{
    session_start();
    session_destroy();
    session_start();
    header("Location: $url");
}

如果您想根据用户是否登录来显示不完整的内容,那么您可以简单地检查每个页面上是否设置了登录会话,而不是为此修改标题。

于 2013-02-19T22:27:42.280 回答
0

将当前会话传递到下一页...我相信这就是您要问的...

您当前没有将会话传递到下一页并session_start()在下一页顶部使用。

将第 4 行更改为:

{ $_SESSION['uid']=$Xid;header('Location: '.$uri.'?'.SID.'&page=welcome'); } // Where "page" is the name of the data you are retrieving

或者,您可以将会话数据保存到 cookie,然后在下一页检索它。

您可以session_start("NameHere")在每个页面上使用时交替命名会话,但是如果访问者最近访问过并且会话没有被破坏,如果您启用了它们,他们可能会看到解析错误。

于 2013-08-09T21:01:28.623 回答