2011-10-04 137 views
0

我遇到問題,我的會話變量未被設置/保存。會話變量未設置在標頭重定向

這裏是我的代碼:

index.php文件有:

<? //this is first line of page 
session_start(); 
?> 
<form action="admin_process_login.php" method="post"> 
<p>EMAIL</p><input name="email" type="text"> 
<p>PASSWORD</p><input name="password" type="password" /> 
<input type="submit" value="Enter"> 
</form> 

admin_process_login.php

<? //this is first line of page 
session_start(); 
$useremail = $_POST['email']; 
$postpassword = $_POST['password']; 
include('admin_config.php'); 

if ($postpassword != "" && $useremail != "") { 
//Connect to database 
mysql_connect("localhost", $dbusr, $dbpass) or die(mysql_error()); 
mysql_select_db("studioel_dental") or die(mysql_error()); 

//Look for a matching email/password 
$query = "SELECT * 
    FROM users 
    WHERE users.email = '$useremail' 
    AND users.password = '$postpassword'"; 

$result = mysql_query($query) or die(mysql_error()); 
$row = mysql_fetch_array($result); 

if(mysql_num_rows($result)==0){ 
    header("Location: http://www.dentalbenefitprogram.com/admin.php?error=nomatch"); 
    } else { 
    //set session variables and load supplies page 
     $uid = $row['id']; 
     $unamex = $row['name']; 
     $uemailx = $row['email']; 
     $utypex = $row['type']; 

     $_SESSION['userid']=$uid; 
     $_SESSION['uname']=$unamex; 
     $_SESSION['uemail']=$uemailx; 
     $_SESSION['utype']=$utypex; 

     header("Location: http://www.dentalbenefitprogram.com/admin_groups.php"); 
     exit; 
     }; 
} else { 
//email or password fields were blank. Return to login page 
header("Location: http://www.dentalbenefitprogram.com/admin.php?error=blank"); 
}; 
?> 

你可能已經猜到:會話變量沒有被設置.. 。

任何幫助都很讚賞!

+0

這正常工作,請創建一個測試腳本添加ob_end_flush();只初始化會話,設置會話變量並執行重定向。測試它是否工作(如果不起作用),如果它不起作用,請在此處替換您的代碼,因爲它包含的內容遠多於您所問的內容。如果有效,請更改您的問題。 – hakre

+0

糟糕。我是個白癡。 讓我撤回我的問題。謝謝你幫忙找到它,Hakre。 我從數據庫中得到結果後忘了做一個while循環。 對不起(感覺羞怯)。 –

回答

1

布萊恩:

我的猜測是,在第一行的評論之前,你必須是造成這一問題的空間。

PHP手冊提醒用戶需要注意的是:

注: 要使用基於cookie的會話,在session_start()必須outputing什麼瀏覽器之前調用。

此外,您可能需要考慮使用長標籤而不是短標籤。

最佳

0

使用ob_start();在我們的代碼的極端開始 &不要忘記在我們的代碼的最末端