会话变量未在标头重定向上设置

时间:2011-10-04 19:19:12

标签: php session variables header

我遇到的问题是我的会话变量没有设置/保存。

这是我的代码:

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");
};
?>

您可能猜到:会话变量未设置...

非常感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

布莱恩:

我的猜测是,在您对第一行的评论之前,您有一个导致此问题的空间。

PHP手册提醒用户注意:

  

注意:   要使用基于cookie的会话,必须在向浏览器输出任何内容之前调用session_start()。

此外,您可能需要考虑使用长标签而不是短标签。

最佳

答案 1 :(得分:0)

在你的代码的最开头使用ob_start(); &安培;别忘了在你的代码的最末端添加ob_end_flush();