2014-10-27 131 views
-2

我正在使用PHP會話變量,但問題在於會話變量沒有結轉。我使用Firebug進行了檢查,發現會話變量根本沒有被創建。 print_r($_SESSION);在頁面返回到index.php後返回一個空數組,但在刷新之前正確顯示它。 logincheck.php:PHP會話變量不能結轉

<?php 
session_start(); 
error_reporting(-1); 
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true) 
{ 
$username=$_SESSION['username']; 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title>Smart Shopping Cart System</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css"> 
    <style type="text/css"> 
    .show-grid{ 
     background:#E0E0E0; 
     }</style> 
</head> 
<body> 
<?php 
include "connection.php" 
?> 


//  ** Some Content** 
</body> 
</html> 
<?php 
} 
else 
{ 

    ?> 
    <html> 
<head> 
    <title>Log In | SmartCart</title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" type="text/css" href="css/bootstrap.css"> 
</head> 
<body> 

    <div class="navbar navbar-default"> 
     <div class="container"> 
      <div class="navbar-brand"> 
       SmartCart 
      </div> 

     </div> 
    </div> 

    <div class="container"> 
     <br /> 
     <div class="row"> 
      <div class="col-md-4"> 
      </div> 
      <div class="col-md-4"> 
       <form role="form" method="post" name="login" action="test.php"> 
        <div class="form-group"> 
         <label for="username">Operator Username</label> 
         <input type="username" class="form-control" name="username" placeholder="Enter your username"> 
        </div> 
        <div class="form-group"> 
         <label for="pass">Password</label> 
         <input type="password" class="form-control" name="pass" placeholder="Password"> 
        </div> 


        <button type="submit" class="btn btn-default" name="sub">Submit</button> 
       </form> 
      </div> 
      <div class="col-md-3"> 
      </div> 
     </div> 
    </div> 

</body></html> 
    <?php 

} 
?> 

test.php的

<?php 
session_start(); 
error_reporting(-1); 
?> 
<html><head><title>Redirecting...</title> 
</head> 
<body> 
Redirecting.... 
<?php 
include "connection.php"; 

if(isset($_POST['sub'])) 
{ 
    $u_name=$_POST['username']; 
    $password=mysql_escape_string($_POST['pass']); 
    $password=mysql_escape_string($password); 
    $w="select * from operators where operator_username = '$u_name' AND operator_pass = '$password'"; 
    $b=mysql_query($w) or die(mysql_error()."in query $w"); 
    $num_rows = mysql_num_rows($b); 
    if($num_rows > 0) 
    { 

     $_SESSION['logged_in']=true; 
     $_SESSION['username']=$_POST['username']; 
     print_r($_SESSION); 
     echo '<meta http-equiv="refresh" content="0; url=index.php">'; 
    } 
    else 
    { 
     echo "<script> 
      alert('Your username or password is incorrect. Please try again'); window.location = 'index.php'; </script>"; 
    } 
} 

?> 
</body></html> 

請幫助。

+0

檢查兩個腳本中的'session_id()'。如果你得到不同的值,那麼你的會話cookie設置幾乎肯定是錯誤的,並且你正在獲得不同的/空的會話。 – 2014-10-27 18:23:29

+0

session_id()是相同的。我使用Firebug進行了檢查。 – user3736335 2014-10-27 18:24:21

+0

查看$ _SESSION [「loggedin」] = true; – avoliva 2014-10-27 18:27:05

回答

0

我覺得下在test.php頁面上缺少分數 in $_SESSION['loggedin']=true; 改變這$_SESSION['logged_in']=true;

正如你在

if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true) 

使用這種有與你會話的一部分誤差可能在你DB訪問部分檢查你的數據庫連接沒有錯誤。你可以通過評論查詢部分檢查你的會話,它工作正常。

+0

這是一個錯誤,雖然改變它並沒有改變任何東西。會話變量不會被結轉(或者不是首先創建的)。 – user3736335 2014-10-27 18:45:05

+0

你的會話部分沒有錯誤,錯誤可能是你的db訪問部分檢查你的數據庫連接。你可以通過評論查詢部分檢查你的會話,它工作正常。 – 2014-10-27 18:59:52

+0

該死!我的connection.php中有session_destroy()。我不知道我爲什麼寫這個。謝謝。你能否在你的回答中加入這個評論? – user3736335 2014-10-27 19:03:38

1

現貨的區別:

logincheck:

if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==true) 
        ^^^^^^^^^ 

測試:

$_SESSION["loggedin"]=true; 
       ^^^^^^^^ 

這些事情之一是不喜歡其他的...

+0

我編輯了代碼,並在粘貼時犯了一個錯誤。只是糾正它。這不是什麼導致錯誤。 'print_r($ _ SESSION);'在頁面返回到index.php後返回一個空數組,但在刷新之前正確顯示它。 – user3736335 2014-10-27 18:28:48

+0

然後在php.ini級別啓用調試輸出。在腳本內部執行error_level毫無意義,特別是在session_start()調用之後。你可能有一個「頭文件已發送」的問題,從來沒有看到它,因爲你不打開報告,直到它爲時已晚。 – 2014-10-27 18:30:18

+0

哦,你很容易受到SQL注入攻擊。你是** DOUBLE **編碼的密碼,永遠不會逃避用戶名。確保你的查詢實際上正常工作,並首先設置你的會話變量。 – 2014-10-27 18:31:10