This question already has an answer here:
- PHP - setcookie(); not working 4 answers
I'm trying to write a simple login page for PHP which sets a cookie with the email address value when the user logs in.
setcookie() doesn't seem to be working though... and I do not know why.
Here's the login form:
<?php include 'header.html' ?>
<div id="maincontent">
<h3>Please enter your login information below</h3>
<FORM method="post" action="login.php">
Salesperson Email: <INPUT TYPE="TEXT" name="email" size="20">
Password: <INPUT TYPE="PASSWORD" name="password" size="20">
<INPUT TYPE="submit" name="submit" value="Login">
</form>
<?php print_r($_COOKIE); ?>
</div>
<?php include 'footer.html' ?>
And here's the login file:
<?php
$username = "root";
$password = "root";
$hostname = "localhost";
//connection to the database
$conn = @mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
if (mysql_select_db("w_c_a", $conn)) {
echo ("<p>Database selection successful</p>");
}
else {
die ("Could not locate w_c_a database" .mysql_error());
}
// Select salesperson that matches both username and password
$sql = "SELECT * FROM salesperson WHERE email= '$_POST[email]' AND password = '$_POST[password]'";
$result = mysql_query($sql,$conn) or die(mysql_error());
// if there's a value returned, assign the values to variables.
if (mysql_num_rows($result) == 1){
$name = mysql_result($result, 0, 'name');
$email = mysql_result($result, 0, 'email');
$id = mysql_result($result, 0, 'salesperson');
// set a cookie with the email as a value which expires in a day
setcookie('logged_in', $email, time() + (86400 * 30));
//spit out cookie value
echo $_COOKIE['logged_in'];
} else {
echo ("<p>Incorrect username or password</p>");
echo ("<p>Please try <a href=employee_login.php>logging in again</a></p>");
exit;
}
?>
<?php include 'header.html' ?>
<div id="maincontent">
<?php
print_r($_COOKIE);
echo ("name" . $name . "email" . $email . "id" . $id . "<br>")
?>
</div>
<?php include 'footer.html' ?>
From what I can gather searching around, setting the cookie with the email value should be fine. Nothing is displaying though, even when I try to set it with dummy data.
I've tried clicking through to the next page etc. No dice.
I have the print_r($_COOKIE) value down the bottom to check all cookies.
What am I missing?
</div>