Reputation: 527
I can't figure out why these sessions are not being set. My code seems to be the same as all the tutorials - and stack overflow questions - but it just won't set a session. Please tell me if I'm going wrong somewhere.
Here's the basics: I have a hmtl page with a form that adds an email address and a password to a database. After the insert to the db, I'm trying to start a session, then redirect the user to a new page and check if the session is set. If there's no session the user gets redirected away from the protected page.
This is the code that runs the database insert and is supposed to set the session, but doesn't:
<?php
// connect to db manager
$link = mysql_connect("localhost", "root", "");
if (!$link) {die('Database Error: Could not connect: ' . mysql_error());}
// select db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
// username and password sent from form
$em = mysql_real_escape_string($_POST['email']);
$pw = mysql_real_escape_string($_POST['password']);
// MySQL Insert Statement
$insert = "INSERT INTO users (`email`, `password`) VALUES ('$em',AES_ENCRYPT('$pw','SecretKey'))";
// Perform Query
$execute = mysql_query($insert);
// if inserted, start session + variable, then redirect
if ($execute) {
session_start();
$_SESSION["signUp"];
echo '<script type="text/javascript">';
echo 'document.location.href = "../u/user.php";';
echo '</script>';
}
else{
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
mysql_close($link);
?>
And this is the code at the top of the protected page:
<?php
session_start();
if (empty($_SESSION['signUp'])){
echo "Session not set!";
// header("location:../index.php");
}
?>
I have the redirect commented out so that I can test if the session is being set. Every time I run the form I get redirected to the ../u/user.php"; page and I get the error message saying that the session is not set.
Upvotes: 0
Views: 48
Reputation: 929
you need to set a value to $_SESSION['signUp']
maybe something like $_SESSION['signUp'] = true;
Upvotes: 2
Reputation: 7294
Hi you have not assigned any value to your session
if ($execute) {
session_start();
$_SESSION["signUp"];// where is the value for this ????
// $_SESSION["signUp"] = true; or whatever you want
echo '<script type="text/javascript">';
echo 'document.location.href = "../u/user.php";';
echo '</script>';
}
Also start seesion at top of your page
Upvotes: 3