Jump to content

Archived

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

akrytus

Logoff

Recommended Posts

OK,  i have user login just fine and everything works great.  Now I am trying to create a logoff button to destroy the current session.  To do this I added:

[code]<a onMouseDown="<? session_destroy(); // Logoff Button ?>" href="http://www.nocrs.net"><img src="../Images/Blank%20Links/Submit_Blank.gif" alt="Logoff" width="33" height="20" border="0"></a>[/code]

This worked at first, then it started to destroy the session all on its own, with out having to click the button.  Is there a better way to do this or can you inform me what I have done wrong?

Share this post


Link to post
Share on other sites
just make a form have only the submit button in it that makes them logoff then use this code

[code]if(isset($_POST['logout'])){
$_SESSION = array();
SESSION_DESTROY();
header ("location: Location after logout");
}[/code]

Share this post


Link to post
Share on other sites
You can't execute php code from javascript, which is what you are trying to do. 
[code]onMouseDown="<? session_destroy(); // Logoff Button ?>" [/code]
As legohead said, you have to post to a page that destroys the session, then send them back to the page.

Share this post


Link to post
Share on other sites
[quote author=hitman6003 link=topic=103015.msg409738#msg409738 date=1154717535]
You can't execute php code from javascript, which is what you are trying to do. 
[code]onMouseDown="<? session_destroy(); // Logoff Button ?>" [/code]
As legohead said, you have to post to a page that destroys the session, then send them back to the page.
[/quote]

Ahh, but you can.  That is why my session keeps getting destroyed!  What I dont understand is why it worked for a while, but now does not.  I guess I will have to use a form. Thanks for the help!

Share this post


Link to post
Share on other sites
It's not executing when the user presses the link in the page.  the session_destroy is executed when the page loads, regardless of wether the user clicks the link.

Share this post


Link to post
Share on other sites
No need for a form.

make this [b]logout.php[/b]
[code]<?php
session_destroy();
header("Location: http://www.nocrs.net");
?>[/code]

Logout link:
[code]<a href="logout.php"><img src="../Images/Blank%20Links/Submit_Blank.gif" alt="Logoff" width="33" height="20" border="0"></a>[/code]

Share this post


Link to post
Share on other sites
When  you log out a user you must unset the session or the user will use the back button and be still  be logged in.

example below

log the user out with a get method link.
[code]
<a href="logout.php?logout=user">log out</a>
[/code]

[code]
<?php session_start();

if($_GET['logout']=="user"){

session_destroy();

unset($username);//session name.
unset($password);//session name.
unset($user_id);// session name.

// send user back to index page as properly logged out.
header("location: index.php");
}

?>
[/code]

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.