Jump to content

Archived

This topic is now archived and is closed to further replies.

perezf

How do i make a logout-SOLVED

Recommended Posts

how can i have a user logout after they log in
i am using cookies

Share this post


Link to post
Share on other sites
From the manual...

<?php
// set the expiration date to one hour ago
setcookie ("TestCookie", "", time() - 3600);
setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);
?>

For more info, [url=http://us3.php.net/manual/en/function.setcookie.php]http://us3.php.net/manual/en/function.setcookie.php[/url]

Share this post


Link to post
Share on other sites
can some one make that in to a link for me so the user can click

Share this post


Link to post
Share on other sites
[code]<a href="logout.php">Log out</a>[/code]

And look at gijew's code and/or read the setcookie link offered in the same post to write the one line php script for logout.php

Share this post


Link to post
Share on other sites
so look here is my code to make the cookie now how do i unset it so i can logout
[code] $cookie_name = "auth";
$cookie_value = "ok";
$cookie_expire = "0";
$cookie_domain = "2fr3sh.com";
setcookie($cookie_name, $cookie_value, $cookie_expire, "/", $cookie_domain, 0);[/code]

Share this post


Link to post
Share on other sites
i tried what you guys said and it didnt work
??? ??? ???

Share this post


Link to post
Share on other sites
perezf...

Just use the same code you had in your previous post that I just helped you with.

All you need to do is set the cookie expire to sometime in the past...

$cookie_expire = time()-1;

As for a link, stick the cookie code in a seperate file with a header redirect at the bottom, and then link to it normally.

Regards
Rich

Share this post


Link to post
Share on other sites
I'm trying to work up some good charma so someone can help me out in another post so let me explain that one line of code = )

setcookie ("TestCookie", "", time() - 3600, "/~rasmus/", ".example.com", 1);

setcookie ( string name [, string value [, int expire [, string path [, string domain [, bool secure [, bool httponly]]]]]] )

The first line is a copy/paste from above.  The second line is another copy/paste from the manual telling you what goes in the function.

So this is how the first function works...
- "TestCookie" = string name or the name of the cookie.
- "" = blank.  I believe if you're trying to delete a portion of the cookie (I don't really use them so I'm not positive).
- time() - 3600 = This is setting the time backwards so the browser will delete the cookie.
- "/~rasmus/" = the path you are deleting from (Optional)
- "example.com" = the domain you are deleting from (Optional)
- 1 = If the site is secure or not (Optional)

To sum that up, just make sure to have the cookie name (which you have when you create the cookie) and the time is required as well to delete it.  Follow the first example.  Copy and paste it, change out the name for your cookie and BAM.  That last sentence says it all!

Share this post


Link to post
Share on other sites
I have the following code which both sets and 'unsets' a cookie... If you call cookie.php?status=logout then you get the cookie unset, if you call it with cookie.php?status=login then the cookie gets set.

[code=php:0]
<?php
// Cookie parameters
$cname = "auth";
$cvalue = "yes";
$cpath = "/";
$cdomain = "xxxxxx.co.uk";
$cexpire = (isset($_GET['status'] == "logout") ? time()-3600 : time()+3600; // Ternary to see if we're setting or unsetting

setcookie($cname, $cvalue, $cexpire, $cpath, $cdomain);

// Now we've set the cookie, lets direct to a page that uses it.
header("Location: index.php");
?>
[/code]

And before anyone says anything, I'm aware that you could pass any value other than 'logout' as an argument to cookie and it would set the cookie.  I just cobbled it together quickly to show how simple it is.

Regards
Rich

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.