hey people,
Today I've been playing around with cookies for the first time, all was going well, but I've discovered a bug and I'm not sure what's causing it :S
I have a file called index.php which looks like this:
<?php
session_start();
//Remember User?
if ($_POST['rememberuser'] == 'yes') {
setcookie("theUser", $_POST['username']);
}
else if ($_POST['rememberuser'] == 'no'){
setcookie("theUser", "", mktime(12,0,0,1, 1, 1990));
}
include('Login.php');
?>
As you can see, the index holds the login which in short (meaning I'm only showing the parts that deal with the cookie) looks like this:
//Is cookie set?
if (isset($_COOKIE['theUser'])) {
$username = $_COOKIE['theUser'];
$rememberuser = 'checked="checked"';
}
else {
$username = $_POST['username'];
$forgetuser = 'checked="checked"';
}
if (!isset($_SESSION['valid_user'])) {
echo '
<form method="post" action="index.php?menu=login">
Username: <input name="username" type="text" size="30" value="'.$username.'"/><br />
Password: <input name="password" type="password" size="30" /><br />
<input name="rememberuser" type="radio" value="yes" '.$rememberuser.' /> Remember me<br />
<input name="rememberuser" type="radio" value="no" '.$forgetuser.' /> Always ask for username and password<br />
<input type="submit" value="Sign In" class="signin" />
</form>';
}
So I type username and password, click the 'remember me' radio button and then login and then I log out, return to the index.php and my username is in the username field and the 'remember me' is checked.
So I thought that was it... but apparently not. The problem is, when I close the browser and open a new one and go to index.php, the username field is not pre-filled and the 'Always ask for username and password' radio field is checked.
Does anyone know what I'm doing wrong?
Thanks.