Forum Moderators: coopster

Message Too Old, No Replies

Cookies Query - logging out....

         

woldie

3:00 pm on Sep 21, 2004 (gmt 0)

10+ Year Member



Hi,

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.

dreamcatcher

5:46 pm on Sep 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try echoing $bookadmin to see if it holds any value. If not, you need to use the superglobal $_COOKIE as you probably have register globals off.

$_COOKIE['bookadmin'];

:)

woldie

7:47 am on Sep 22, 2004 (gmt 0)

10+ Year Member



Thanks Dreamcatcher,

I see what you mean, I did try to echo out the value of the cookie, and yes there is no value.

But where do I define $_COOKIE['bookadmin2'] globally?

For example, do I define it in the logout script?

Cheers :)

dreamcatcher

8:23 am on Sep 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To set the cookie simply use:

setcookie("bookadmin","ok",time()-60*60*24*365);

No need for the variables.

Then you would access this variable as $_COOKIE['bookadmin'] and just use it wherever you need to:

if ($_COOKIE['bookadmin']=="ok")
{#//do something
}

Hope that helps.

woldie

10:22 am on Sep 22, 2004 (gmt 0)

10+ Year Member



Thanks again for the response.

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.

kumarsena

12:00 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



why not use sessions instead?

[microcyb.com...]
[phptutorial.info...]
[codewalkers.com...]

kumar