Foser Posted June 26, 2007 Share Posted June 26, 2007 I'm not sure if this exists, because I haven't heard of anything like this. But I'm trying to do logout function with session and want to do something like. The only way I would think of doing it. would click send to a different page something like. <?php session_destroy(); if ($_SESSION['LOGGEDIN'] == FALSE){ header(Location: /login.php); exit;} else { header(Location: /account.php);} ?> is there a way so onclick of (this) will do {this function}? or my way is the only way? Quote Link to comment Share on other sites More sharing options...
teng84 Posted June 26, 2007 Share Posted June 26, 2007 thats possible first tell us wat you exactly want Quote Link to comment Share on other sites More sharing options...
bigbob Posted June 26, 2007 Share Posted June 26, 2007 yes here is how: <?php function logout() { session_destroy(); if ($_SESSION['LOGGEDIN'] == FALSE){ header('Location: /login.php'); exit;} else { header('Location: /account.php'); } } ?> <html> //This is your form <form id="form1" name="form1" method="post" action=""> <label></label> <input type="submit" name="logout" id="logout" value="Logout" onClick="logout()" /> </form> </html> Please note that when you call the "HEADER" command it is always followed by (' and closed by '); so u would change header(Location: /login.php); to header('Location: /login.php'); Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 26, 2007 Share Posted June 26, 2007 bigbob - Are you sure you can mix JavaScript with PHP like that? I'm thinking you can't....maybe I'm wrong though. If that doesn't work, You could use a submit button and it won't require the onclick function. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 26, 2007 Share Posted June 26, 2007 Do this: <?php if ($_POST['submit']){ session_destroy(); if (!isset($_SESSION['LOGGEDIN'])){ header('Location: /login.php'); } else { header('Location: /account.php'); } } ?> <form method="post" action="<?=$_SERVER['PHP_SELF'] ?>"> <input type="submit" name="logout" id="logout" value="Logout"> </form> Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 26, 2007 Share Posted June 26, 2007 If you want to test for FALSE, I'm pretty sure you have to use ===. Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 If you want to test for FALSE, I'm pretty sure you have to use ===. Besides the fact that that is incorrect.. whats does it have to do with the op's question here? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 26, 2007 Share Posted June 26, 2007 If you want to test for FALSE, I'm pretty sure you have to use ===. Besides the fact that that is incorrect.. whats does it have to do with the op's question here? WOO! I smell smoke. charlieholder was just trying to point out a flaw in their code....or trying to at least. No harm in that. Quote Link to comment Share on other sites More sharing options...
mkoga Posted June 26, 2007 Share Posted June 26, 2007 If you destory the session with session_destroy(), can you still store in the global variable $_SESSION? Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 26, 2007 Share Posted June 26, 2007 If you destory the session with session_destroy(), can you still store in the global variable $_SESSION? 0_0 Are you sure you just posted on the right thread? ...if you have a question totally unrelated to the topic, you should probably post your own thread for it. Quote Link to comment Share on other sites More sharing options...
cooldude832 Posted June 26, 2007 Share Posted June 26, 2007 you can run a session destroy via an ajax's query and just return a message saying your are logged out. Quote Link to comment Share on other sites More sharing options...
mkoga Posted June 26, 2007 Share Posted June 26, 2007 oops, read the $_SESSION['LOGGEDIN'] == FALSE a little too fast. Quote Link to comment Share on other sites More sharing options...
bigbob Posted June 26, 2007 Share Posted June 26, 2007 bigbob - Are you sure you can mix JavaScript with PHP like that? I'm thinking you can't....maybe I'm wrong though. If that doesn't work, You could use a submit button and it won't require the onclick function. Im pretty sure that that will work. Try it though. Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 Im pretty sure that that will work. Try it though. No It wont. Javascript looks for functions on the client, PHP runs on the server. Quote Link to comment Share on other sites More sharing options...
bigbob Posted June 26, 2007 Share Posted June 26, 2007 but we arent doing a javascript function...we are doing a php function. Quote Link to comment Share on other sites More sharing options...
trq Posted June 26, 2007 Share Posted June 26, 2007 Exactly. the onClick event is Javascript, and is unable to call PHP on the server. You would need to make the onClick make a request back to the server. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted June 26, 2007 Share Posted June 26, 2007 I usually pass a value with the GET method for logging out. Something like ?logout=1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.