2012-10-26 94 views
0

我想在登錄頁面中存儲會話變量,然後將其傳遞到成員頁面,但由於某些原因,當您轉到成員頁面時會話變量不存儲。會話變量不會傳遞到成員頁面

<?php 
error_reporting(E_ALL^E_NOTICE); 
session_start(); 
$userid = $_SESSION['userid']; 
$username = $_SESSION['username']; 

?> 

然後一堆html。那麼接下來的PHP代碼是...

<?php 

$form = "<form action='login.php' method='post'> 
<table> 
<tr> 
<td><input type='text' name='user' /></td> 
</tr> 
<tr> 
<td><input type='password' name='password' /></td> 
</tr> 
<tr> 
<td><input type='submit' name='loginbtn' value='Login' /></td> 
</tr> 
</table> 
</form>"; 

if ($_POST['loginbtn']) { 
$user = $_POST['user']; 
$password = $_POST['password']; 

if ($user) { 
    if ($password){ 
     require("connect.php"); 

     $password = md5(md5("kjfiufj".$password."GSA54")); 
     //make sure login info is correct 
     $query = mysql_query("SELECT * FROM users WHERE username='$user'"); 
     $numrows = mysql_num_rows($query); 

     if ($numrows == 1) { 
      $row = mysql_fetch_assoc($query); 
      $dbid = $row['id']; 
      $dbuser = $row['username']; 
      $dbpassword = $row['password']; 
      $dbactive = $row['active']; 

      if ($password == $dbpassword) { 
       if ($dbactive == 1) { 
       //set session info 
        $_SESSION['userid'] = $dbid; 
        $_SESSION['username'] = $username; 


        echo "logged in as  <strong>$dbuser</strong>. <a href='member.php'>Click here</a> to go to the member page."; 
       } 
       else 
        echo "User not active"; 
      } 
      else 
       echo "Wrong password."; 
     } 
     else 
      echo "The username you entered was not found"; 

     mysql_close(); 
    } 
    else 
     echo "What's the password brah?"; 


} 
else 
    echo "What's the user name brah?"; 



} 
else 
    echo $form; 
?> 

爲會員頁面的代碼

<?php 
error_reporting(E_ALL^E_NOTICE); 
session_start(); 
$userid = $_SESSION['userid']; 
$username = $_SESSION['username']; 

?> 

<?php 
if ($username && $userid) { 
    echo "Welcome <strong>$username</strong>,<a href='logout.php'>Logout</a>."; 
} 

else 
    echo "Please login to acces this page <a href='login.php'>Login here</a>"; 



?> 
+0

這個位做了什麼「if($ username && $ userid){」 – case1352

+0

它應該檢查它們是否存在 – Philip

+0

它們確實存在,因爲您剛剛在上面的行中創建它們。不管會話中是否設置了任何內容。檢查會話變量是否爲空。或在會話變量 – case1352

回答

2

你叫你的登錄頁面session_start()

如果未將session.auto_start運行時配置選項設置爲true,則必須在每個要訪問會話變量的頁面中撥打session_start()


編輯

其實,你可能$_SESSION['userid']不是空的,因爲你實際上分配定義的變量,以它作爲$_SESSION['userid'] = $dbid;

但是,您創建$_SESSION['username']通過將值$username它似乎是未定義的。

也許

$_SESSION['username'] = $username; 

應該

$_SESSION['username'] = $dbuser; 
+0

他正在調用session_start() – case1352

+0

沒錯。我首先想到兩個第一代碼塊是不同的文件。我的錯。 – budwiser

0

檢查這兩個變量實際上包含的東西

session_start(); 
$userid = $_SESSION['userid']; 
$username = $_SESSION['username']; 

?> 

<?php 
if (strlen($username) >0 && strlen($userid)>0) { 
    echo "Welcome <strong>$username</strong>,<a href='logout.php'>Logout</a>."; 
} 

或檢查會話變量被設置

session_start(); 
$userid = $_SESSION['userid']; 
$username = $_SESSION['username']; 

?> 

<?php 
if (isset($_SESSION['username']) && isset($_SESSION['userid'])) { 
    echo "Welcome <strong>$username</strong>,<a href='logout.php'>Logout</a>."; 
}