Brian Barrus
Brian Barrus

Reputation: 515

Session variables not set on header redirect

I'm having a problem where my session variables aren't getting set/saved.

Here is my code:

index.php has:

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

You can probably guess: the session variables aren't being set...

Any help is appreciated greatly!

Upvotes: 0

Views: 2934

Answers (2)

iThink
iThink

Reputation: 1259

use ob_start(); at the extreme beginning of ur code & dont forget to add ob_end_flush(); at the extreme end of ur code

Upvotes: 0

jeff musk
jeff musk

Reputation: 1022

Brian:

My guess is that before your comment in the first line you have a space that is causing this problem.

PHP manual cautions users to watch out for this:

Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

Also, you may want to consider using long tags instead of short tags.

Best

Upvotes: 1

Related Questions