Jump to content

How do i make a logout-SOLVED


perezf

Recommended Posts

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]
Link to comment
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]
Link to comment
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
Link to comment
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!
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.