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? Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/ 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 Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282660 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'); Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282683 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. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282685 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> Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282688 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 ===. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282709 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? Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282713 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. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282715 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? Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282716 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. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282717 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. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282723 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. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-282725 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. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-283039 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. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-283043 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. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-283048 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. Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-283083 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 Link to comment https://forums.phpfreaks.com/topic/57191-onclick-functions/#findComment-283095 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.