Click to See Complete Forum and Search --> : Logout button for forum


spykemitchell
10-17-2003, 02:41 PM
I want to create a logout function for a forum, the forum uses a cookie to check wheteher the user is logged in, i need to make a link that will delete the cookie and then take them to another page after clicking it... Any ideas?

The cookie set is,


setcookie("cusername",$lusername,time()+1800);


(logs out automatically after 30mins)

PunkSktBrdr01
10-17-2003, 03:11 PM
Try this:


<?
setcookie("cusername",$lusername,time()-1800);
header("Location: another_page.php");
?>

spykemitchell
10-18-2003, 04:51 AM
and that will delete the cokie then? Wow simple! PHP isn't as hard as i though it was...

Thanks...

PunkSktBrdr01
10-18-2003, 09:45 AM
Glad to help!

spykemitchell
10-18-2003, 11:58 AM
Now if i only wanted to make it show (as a link) when a user is logged in, how would i do that?

Would the link be...



<a href="<?
setcookie('cusername',$lusername,time()-1800);
header('Location: another_page.php');
?>">Log out</a>



How would i get it to check for the cookie before showing this link?

eomer
10-18-2003, 12:24 PM
That would be effective, but the long way around. In other words if you wanted to put it on multiple pages then you have to add all that code to it. Then you want to change one part of the logout... lots of work for you. Try this:

function logout($page, $lusername)
{
setcookie('cusername',$lusername,time()-1800);
header('Location: $page');
}

Now that is a bit incomplete. You will want to verify that the data is there, and is valid blah blah blah.
Then you would simply write the html like:
<a href="<?logout([page_redirect], [username]);?>Logout</a>. Now this gives you the ability to add it anywhere....and tell it to redirect to a page. Usually what I do in the checking phase is if those variables are blank put a default page to go to...or give an error msg for the username. That's up to you.

PunkSktBrdr01
10-18-2003, 12:33 PM
That wouldn't work. PHP is server-side, so you cannot execute on the client's machine. You would need to create a page like this for the logout:


<?
if(!isset($_COOKIE['cusername'])) {
echo "You cannot access this page because you are not logged in!";
}
else {
setcookie('cusername',$lusername,time()-1800);
header('Location: another_page.php');
}
?>

eomer
10-18-2003, 12:52 PM
You're right my bad. I usually put the html to a page like logout.php and call the function from there. Sorry doing two things at once. However the function will work fine. I use it all the time.

Jona
10-18-2003, 12:57 PM
Originally posted by PunkSktBrdr01
That wouldn't work.

He's right, doing that would give you an error. Namely, "headers already sent out." The reason is because you must do all header processing before the page begins loading. Setcookie() and Header() functions both are header functions, and therefore having any data output to the browser before calling the functions will result in an error. A logout link would be a simple link to a PHP document which deletes the cookie and sends you back to the index... I'd give you code, but PunkSktBrdr01 already nailed it. ;)

Edit: I just saw your reply, eomer. I posted this before I realized you had replied.

[J]ona

eomer
10-18-2003, 01:08 PM
Not a problem. I'm just smoking crack today. I know both of these things. Like I said I have a function very similar to these, but I guess I just stuck my foot in my mouth.:eek: Nothing new here though.
Thanks for editing though and lettin' me know you weren't just dissin' me. :D

Jona
10-18-2003, 01:13 PM
Yeah, I hope I didn't sound mean there... Just kind of reiterating what PunkSktBrdr01 said... Trying to make it a bit more clear for ya'. I didn't realize what you knew already either, so... ;)

[J]ona

eomer
10-18-2003, 01:15 PM
Like I said....smokin' crack...or at least I feel like it. :D
You're right...know way of knowing what I know and what I don't know. However, I must admit don't know is much larger then do know.

spykemitchell
10-18-2003, 04:47 PM
Ok so, what do i need to do and where do i need to put it?

PunkSktBrdr01
10-18-2003, 05:19 PM
To use my code, just paste it in a new file and save it as "logout.php". For the link, just link to logout.php.

spykemitchell
10-19-2003, 10:48 AM
Thanks!