emopoops Posted May 20, 2009 Share Posted May 20, 2009 i made a login script with the ability to acess the members only page if ur logged in correctly (the session set to true) now when i go to my singout page which has this code i can still acess my members only page and am still signed in? how do i sign out? <? session_start(); session_destroy(); ?> isnt working Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/ Share on other sites More sharing options...
eRott Posted May 20, 2009 Share Posted May 20, 2009 <?php session_unset(); session_destroy(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-837763 Share on other sites More sharing options...
MadTechie Posted May 20, 2009 Share Posted May 20, 2009 try <?php session_start(); $_SESSION = array(); if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-42000, '/'); } session_destroy(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-837764 Share on other sites More sharing options...
MadTechie Posted May 20, 2009 Share Posted May 20, 2009 eRott, you will still need to Initialize the session first Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-837765 Share on other sites More sharing options...
void Posted May 20, 2009 Share Posted May 20, 2009 or, probably session_start(); session_destroy(); unset($_SESSION); :-) Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-837767 Share on other sites More sharing options...
MadTechie Posted May 20, 2009 Share Posted May 20, 2009 Caution Do NOT unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal. Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-837770 Share on other sites More sharing options...
eRott Posted May 20, 2009 Share Posted May 20, 2009 eRott, you will still need to Initialize the session first Aye. Sorry, I probably should have clarified that. Yes, I know it needs to be initialized first. Myself, I use it as a function. function user_logout() { // End the session and unset all vars session_unset(); session_destroy(); } The session is already started elsewhere on the page. Sorry for the confusion. Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-837786 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Author Share Posted November 19, 2009 session_start(); //yes, you still have to start the session session_unset(); session_destroy(); that is what im using. i dont understand the caution not to use the unset() Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960563 Share on other sites More sharing options...
MadTechie Posted November 19, 2009 Share Posted November 19, 2009 I mean it trashes the session ID, (could cause problems with other sessions etc) basically bad practice using session_unset is fine however I would recommend you us this as it also clears the cookie as sometimes the sessions don't clear from the server this will also clear the link from the client try <?php session_start(); $_SESSION = array(); if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-42000, '/'); } session_destroy(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960569 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Author Share Posted November 19, 2009 do i have to use the 420000 can i change the number? Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960574 Share on other sites More sharing options...
MadTechie Posted November 19, 2009 Share Posted November 19, 2009 Sure that just sets the cookies expire time to a time in the pasted (forces it to delete itself) Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960578 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Author Share Posted November 19, 2009 session_start(); //yes, you still have to start the session session_unset(); session_destroy(); if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time()-55, '/'); } so i dont have to add ANYTHING ELLSE? that is taken from the page exactly to signout. ? Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960580 Share on other sites More sharing options...
MadTechie Posted November 19, 2009 Share Posted November 19, 2009 add $_SESSION = array(); under session_start() it will clear the session details, yes i know your deleting them but its a just in case measure, you could also redirect them to the home page ie at the end add header("Location: index.php"); Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960586 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Author Share Posted November 19, 2009 i have a header url thanks. i still dont get what the array means tho but ill add it Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960589 Share on other sites More sharing options...
MadTechie Posted November 19, 2009 Share Posted November 19, 2009 all your doing is removing details then removing the item itself Now just say the session didn't get removed from the server (for whatever reason) that means the users details are still floating around on the server waiting to timeout right ? Well by setting the details to an empty array they are no use to anyone so it doesn't matter as much compared to then holding valid user details. Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960597 Share on other sites More sharing options...
emopoops Posted November 19, 2009 Author Share Posted November 19, 2009 oh so its changing them to empty? is it ok to change the details to empty before doing the unset and destroy and cookie check? Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960610 Share on other sites More sharing options...
MadTechie Posted November 19, 2009 Share Posted November 19, 2009 Yes you should set it to empty before then as you do want it unset/removed, setting it to empty is just incase they fail. Quote Link to comment https://forums.phpfreaks.com/topic/158835-solved-why-wont-log-me-out/#findComment-960612 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.