Forum Moderators: coopster
I've got a small problem with logging out with cookies. I have posted something like this before, but I just want to add something a little extra.
What I have is a login form, and then it points to a login script, where it queries the DB and then sets a cookie.
Login script:
$result=mysql_query("select baid from tablename
where email_address='$email'
AND password=password('$pass')
AND employed=1");
list($DBbaid)=mysql_fetch_row($result);
if (mysql_num_rows($result)!=0)
{
$cookie_name = 'bookadmin';
$cookie_value = 'ok';
setcookie("BAID",$DBbaid);
setcookie ($cookie_name,$cookie_value, time()+3200);
header("Location: index.php");
exit;
}
else
{
header("Location:login.html");
exit;
}
This works, and so in index.php I have this snippet of code to say if cookie has been set.
<?
if ($bookadmin == 'ok')
{
// do whatever
}
else
include('redirect-admin.php');
?>
However I would like the user to logout, so I've used this script to do this.
<?
if ($bookadmin == 'ok')
{
$cookie_name = 'bookadmin';
$cookie_value = 'ok';
$time=time();
setcookie($cookie_name,$cookie_value,time()-60*60*24*365);
header("Location:login.html");
exit;
}
else
{
header("Location:login.html");
exit;
}
?>
What happens it goes to the login page, which says you are logged out, however if you straight into index.php after you logged in by typing into the address bar in the browser, it lets you in where by it should redirect you to the login page, yes?
This is the problem I am having, can anyone spot where I am going wrong.
Many Thanks
Wold.
I've already using this line of code:
setcookie("bookadmin","ok",time()-60*60*24*365);
In the logout script, yes?
logout.php
if ($bookadmin == 'ok')
{
$cookie_name = 'bookadmin2';
$cookie_value = 'user';
$time=time();
setcookie($cookie_name,$cookie_value,time()-60*60*24*365);
header("Location:login.html");
exit;
}
else
{
header("Location:login.html");
exit;
}
As for this line of code:
if ($_COOKIE['bookadmin']=="ok")
{#//do something
}
I'm already using this when you first login into the system, because I'm already setting a cookie called bookadmin. This is in the file index.php.
In the index.php file I used this line of code:
global $HTTP_COOKIE_VARS;
echo $HTTP_COOKIE_VARS['bookadmin2'];
When I ran this I got nothing.
Just need to figure out why you do logout that I can still type in index.php but really you should throw you back to the login page.
Thanks for your help.