Jump to content


Photo

How do i make a logout-SOLVED


  • Please log in to reply
10 replies to this topic

#1 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 11 September 2006 - 11:03 PM

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

#2 gijew

gijew
  • Members
  • PipPipPip
  • Advanced Member
  • 240 posts
  • LocationCalifornia

Posted 11 September 2006 - 11:06 PM

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, http://us3.php.net/m...n.setcookie.php
I'm considered good looking in some countries

#3 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 11 September 2006 - 11:08 PM

can some one make that in to a link for me so the user can click

#4 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 11 September 2006 - 11:08 PM

i dont really understand that

#5 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 11 September 2006 - 11:13 PM

<a href="logout.php">Log out</a>

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
Legend has it that reading the manual never killed anyone.
My site

#6 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 11 September 2006 - 11:19 PM

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


#7 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 11 September 2006 - 11:20 PM

i tried what you guys said and it didnt work
??? ??? ???

#8 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 11 September 2006 - 11:21 PM

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#9 perezf

perezf
  • Members
  • PipPipPip
  • Advanced Member
  • 301 posts
  • LocationFort Lauderdale

Posted 11 September 2006 - 11:23 PM

Worked Perfectly Thank you so much HuggieBear

#10 gijew

gijew
  • Members
  • PipPipPip
  • Advanced Member
  • 240 posts
  • LocationCalifornia

Posted 11 September 2006 - 11:25 PM

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!
I'm considered good looking in some countries

#11 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 11 September 2006 - 11:28 PM

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.

<?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");
?>

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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users