2015-09-21 94 views
1

爲了讓我的網址更加美觀,並使多步驟激活過程更加簡單,我編寫了我的頁面,將激活電子郵件中的用戶ID和激活碼存儲爲會話變量。當一個userID和actCode在url中時,它將它們保存爲會話變量,然後重定向到激活(我使用htaccess來取消.php部分)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:[email protected]?Subject=Account%20Activation">[email protected]</a> 
       </p> 
      '; 
     } 
     elseif ($activationExpire < $now){ 
        echo ' 
          <p> 
            Your activation code has expired. Please email 
            <a href="mailto:[email protected]?Subject=Account%20Activation">[email protected]</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

+0

就我所見,您的代碼似乎是正確的,並且會話cookie被髮送到服務器。你可以看看服務器上的php/tmp目錄並打開其中一個會話文件(它們具有會話ID作爲名稱)並查看它們是否包含值?編輯:也嘗試print_r($ _ COOKIE),看看是否有會話cookie – x4rf41

+0

我得到陣列([PHPSESSID] => 170f07f1467f149eda07f3 *******) –

+0

@ x4rf41我應該這樣做,而不是一個會話的cookie變量? –

回答

0

一個原因測試它爲什麼你會話可能會返回意外的行爲。你提到了你正在發行的redirect。但是你是否重定向到相同的域名子域?

如果您的網站正在查詢www和非www版本,那麼您可能會獲得不同的會話,因爲它們被視爲不同的子域。你可以改變你的htaccess來解決這個問題,或者你可以進入你的開發控制檯並輸入document.cookie並比較這兩個頁面進行檢查。