I need help, this is driving me a little up the wall. My PHP cookies are not working correctly.
I have a log on page, that tell you if your logged on or not. Which seems to work first time. User can log on to page, page ids the user using a cookie... fine. That part works, but then if you log out then try to log back in again. The page say the user is still logged in. I've been trying this and that changing this bit of code and that. it just isnt working.
below is three bit of code,
Firstly a simple form send the data to this script called, adminLogin.php
<?php
ob_start();
include("config.php");
// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());
// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());
$match = "SELECT adminID FROM admin WHERE username = '".$_POST['username']."'
and password = '".$_POST['password']."';";
$qry = mysql_query($match)
or die ("Could not match data because ".mysql_error());
$num_rows = mysql_num_rows($qry);
if ($num_rows <= 0) {
echo "Sorry, there is no username $username with the specified password.<br>";
echo "<a href=login.php>Try again</a>";
exit;
} else {
setcookie("adminloggedin", "TRUE", time()+(3600 * 24));
setcookie("mysite_username", "$username");
echo "You are now logged in!<br>";
echo "Continue to the <a href=admin.php>members</a> section.";
}
ob_end_flush();
?>
Then the admin.php page (which shows you logged in):
<?php
if (!isset($_COOKIE['adminloggedin'])) die("
<p>You are not logged in!<br />
<form action='adminLogin.php' method='post'>
Username: <input type='text' name='username' size='20'><br>
Password: <input type='password' name='password' size='20'><br>
<input type='submit' value='Log In'>
</form>");
$mysite_username = $HTTP_COOKIE_VARS["mysite_username"];
echo "you are logged in as $mysite_username.<p>";
?>
the log out script:
<?php
// expire cookie
setcookie ("adminloggedin", "", time()-3600);
echo "You are now logged out.<br />";
echo "<a href=\"admin.php\">Log in</a>";
?>
which when you got back to the admin page it still says your logged in.
please help
Craig