【问题标题】:PHP session variable shows as empty on refreshPHP会话变量在刷新时显示为空
【发布时间】:2015-12-18 18:22:58
【问题描述】:

为了美化我的 url 并使多步骤激活过程更容易,我对我的页面进行了编程,以将来自激活电子邮件的用户 ID 和激活代码存储为会话变量。当 userID 和 actCode 在 url 中时,它将它们保存为会话变量,然后重定向到激活(我使用 htaccess 去掉了 .php 部分)

它第一次工作(当页面自行刷新时),但是当您移动到不同的步骤或手动刷新页面时,它会删除它们。

这是我的代码:

  <?php
error_reporting (E_ALL ^ E_NOTICE);
session_start();

if ( (!empty($_GET['u'])) && (!empty($_GET['a'])) ) {
    $_SESSION["activate_userID"]        = $_GET['u'];
    $_SESSION["activate_actCode"]       = $_GET['a'];
    header( 'Location:activate') ;
}else{
    $userID = $_SESSION["activate_userID"];
    $actCode = $_SESSION["activate_actCode"];
    echo 'session variable found: '.$actCode;
}

if ($actCode == ""){$actCode = "nUlL";}


require "***connection script***";



$checkCode = "SELECT ***account details***, `activationExpire` FROM `users` WHERE `userID` = \"$userID\"; ";
$result = $conn->query($checkCode);

if ($result->num_rows > 0) {
    // output data of each row
    while($actInfo = $result->fetch_assoc()) {
    *** account details are here ***
    $step               =   $actInfo['activationStatus'];
    $activationCode     =   $actInfo['activationCode'];
    $activationExpire   =   $actInfo['activationExpire'];
    }
}
?>

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Activate - FiestaUSA</title>
    <link href="includes/css/materialize.min.css" type="text/css" rel="stylesheet" media="screen,projection"/>
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

</head>

<body class="blue" background="includes/images/bg.jpg" style="background-size: cover;">
    <div class="row">
        <div class="col s10 m8 l6 offset-s1 offset-m2 offset-l3" style="padding-top: 50px">
            <div class="card-panel z-depth-2 ">
                <div class="row center">
                    <img src="includes/images/white480.png">
                </div>
                <div class="row">

    <?php
                $now = date('Y-m-d H:i:s');
                if($actCode !== $activationCode) {
          echo '
              <p>
                  There was a problem activating your account. Please email
                  <a href="mailto:activation@fiestausa.com?Subject=Account%20Activation">activation@fiestausa.com</a>
              </p>
          ';
        }
        elseif ($activationExpire < $now){
                    echo '
                            <p>
                                    Your activation code has expired. Please email
                                    <a href="mailto:activation@fiestausa.com?Subject=Account%20Activation">activation@fiestausa.com</a>
                            </p>
                    ';

                ;} else {

                    if ($step == 6){
                        header( 'Location:signin') ;
                    }

                    if ($step == 5){
                        require "includes/php/activation/s5.php";
                    }

                    if ($step == 4){
                        require "includes/php/activation/s4.php";
                    }

          elseif ($step == 3){
            require "includes/php/activation/s3.php";
          }

          elseif ($step == 2){
            require "includes/php/activation/s2.php";
          }

          elseif ($step == 1){
            require "includes/php/activation/s1.php";
          }
        }
    ?>
                </div>
            </div>
        </div>
    </div>
</body>

</html>

您可以在以下位置对其进行测试 http://fiestausa.com/myevent/activate.php?u=2&a=fiverr

【问题讨论】:

  • 据我所知,您的代码似乎是正确的,并且会话 cookie 被发送到服务器。您能否查看服务器上的 php/tmp 目录并打开其中一个会话文件(它们以会话 ID 作为名称)并查看它们是否包含这些值?编辑:也试试 print_r($_COOKIE) 看看是否有会话cookie
  • 我得到数组([PHPSESSID] => 170f07f1467f149eda07f3*******)
  • @x4rf41 我应该将其作为 cookie 而不是会话变量吗?
  • 好吧,如果用户编辑信息不是安全问题,您可以这样做。顺便提一句。我刚刚在我的本地服务器上运行了高达if ($actCode == ""){$actCode = "nUlL";} 的会话代码,它没有问题,这是我认为的一些服务器或 php 配置问题。您可以访问 php.ini 或打印 phpinfo() 吗?
  • 在第 6 步中,一旦输出任何 html,就无法更改标题。

标签: php variables session store erase


【解决方案1】:

您的会话可能返回意外行为的原因。您提到了您正在发行的redirect。但是您是否重定向到同一个域 子域?

如果您的网站同时查询 www 和非 www 版本,那么您可能会获得不同的会话,因为它们被视为不同的子域。您可以更改您的 htaccess 来解决此问题,或者您可以通过进入开发控制台并键入 document.cookie 并比较两个页面来进行检查。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-02
    • 2015-10-10
    • 2014-11-27
    • 2014-03-24
    • 2020-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多