hackalive Posted June 4, 2012 Share Posted June 4, 2012 Hi guys, I have a PHP session/cookies that I use as a login mechanism. However it seems that: session_unset(); session_destroy(); is not killing the sessions & cookies any ideas? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/ Share on other sites More sharing options...
silkfire Posted June 4, 2012 Share Posted June 4, 2012 To properly destroy a session, you do like this: $_SESSION = array(); To properly destroy a cookie, set the cookie again but with time somewhere in the past, for example -30 days ago. Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351086 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 I set it like this: session_name('s'); session_set_cookie_params(2*7*24*60*60); session_start(); So how would I destroy that? Or is there any easy way to destroy ALL cookies and sessions for a domain (e.g., ".mydomain.com" - thats how I am setting them all). Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351092 Share on other sites More sharing options...
HDFilmMaker2112 Posted June 4, 2012 Share Posted June 4, 2012 I set it like this: session_name('s'); session_set_cookie_params(2*7*24*60*60); session_start(); So how would I destroy that? Or is there any easy way to destroy ALL cookies and sessions for a domain (e.g., ".mydomain.com" - thats how I am setting them all). Did you call session_start(); before session_unset and session_destroy? If not, it doesn't have know what the values are that it should be unsetting and destroying. session_start should essentially be read as, check to see if there's already a session started, if so continue it; if not, start a new one. Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351095 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 Doing: session_start(); session_unset(); session_destroy(); merely creates a new session named PHPSESSID and does not unset the "s" session or the one it created. This: session_name('s'); session_start(); session_unset(); session_destroy(); Stops it creating the new session I discuss above, but it still does not destroy the "s" session. Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351096 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 This session_name('s'); session_start(); setcookie (session_id(), "", time() - 3600); session_destroy(); session_write_close(); OR session_name('s'); session_start(); setcookie (session_id('s'), "", time() - 3600); session_destroy(); session_write_close(); Does not work either Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351104 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 This is starting to really frustrate me as nothing seems to work! Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351120 Share on other sites More sharing options...
silkfire Posted June 4, 2012 Share Posted June 4, 2012 Hackalive did you ever try my solutions? Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351126 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 Doing logout.php <?php $_SESSION = array(); Does not work. Nor does <? session_name('s'); session_start(); $_SESSION = array(); Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351128 Share on other sites More sharing options...
silkfire Posted June 4, 2012 Share Posted June 4, 2012 What browser are you using? Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351129 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 Firefox on Mac & PC - Also Safari on Mac So its clear: this is how I am setting up the session etc <?php session_name('s'); session_set_cookie_params(2*7*24*60*60); session_start(); $_SESSION['active'] = '1'; $_SESSION['user'] = '50'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351130 Share on other sites More sharing options...
silkfire Posted June 4, 2012 Share Posted June 4, 2012 Hmmmm. Because Google Chrome 19 which I'm using has a stupid functionality introduced in this version to not destroy session cookies when you close the browser. Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351132 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 Well this cookie does not delete on exit because I have set session_set_cookie_params(2*7*24*60*60); But it seems I cant get it to delete at all - even if i dont use the above line (im talking about a log-out function kill here) Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351133 Share on other sites More sharing options...
PFMaBiSmAd Posted June 4, 2012 Share Posted June 4, 2012 Do you have php's error_reporting and display_errors set so that you would know if your session_start() statement is working or not? The session_start would need to be successful before you can modify or delete the corresponding session data. Also, your session_set_cookie_params settings is not setting the cookie path to anything, so the session id cookie will only match the path where it was set. You can then only access that session data in the same path where it was set at. If your log-out code is in a different path from the log-in code, you won't we able to destroy the session data. You should normally set the cookie path to '/' so that the cookie will match all paths under your domain. Also, you should not care if regular/session cookies exist or not to determine if someone is logged in. You should be solely using a value on the server to determine if someone is logged in or not. Doing so will mean that you don't care if a cookie exists or not and you won't need to waste time trying to delete cookies (anyone can make a copy of a cookie and restore it after you have deleted it.) Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351138 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 Do you have php's error_reporting and display_errors set so that you would know if your session_start() statement is working or not? The session_start would need to be successful before you can modify or delete the corresponding session data. Also, your session_set_cookie_params settings is not setting the cookie path to anything, so the session id cookie will only match the path where it was set. You can then only access that session data in the same path where it was set at. If your log-out code is in a different path from the log-in code, you won't we able to destroy the session data. You should normally set the cookie path to '/' so that the cookie will match all paths under your domain. Also, you should not care if regular/session cookies exist or not to determine if someone is logged in. You should be solely using a value on the server to determine if someone is logged in or not. Doing so will mean that you don't care if a cookie exists or not and you won't need to waste time trying to delete cookies (anyone can make a copy of a cookie and restore it after you have deleted it.) Can you provide a link that shows how to implement a login system based on your db-centric idea? Or can you detail out it implementation more? Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351142 Share on other sites More sharing options...
PFMaBiSmAd Posted June 4, 2012 Share Posted June 4, 2012 db-centric idea Nothing I wrote was db-centric. Setting/clearing a server-side session variable to indicate the logged-in/logged-out state is perfectly fine for a simple log in script. A more advanced log in script, with user permissions/roles, a remember-me feature, or the ability to ban users would require that you store the logged in state and permission information in your user table and query that table on each protected page to identify the user (remember-me feature) or to get the user's current state/permissions. Get your current log-in/log-out script working first. Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351145 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 okay well if you can give a link to a login script that uses the db to record if you are in or not - I am yet unable to find such a script Also login.php session_name('s'); session_set_cookie_params('2*7*24*60*60','/'); session_start(); $_SESSION['active'] = '1'; $_SESSION['user'] = '50'; "cookies details" Name: s Content: ..... Domain: mydomain.com Path: / (Domain/Path: .mydomain.com/) - this one from FF on PC - above is FF on Mac logout.php session_name('s'); session_start(); session_unset(); session_destroy(); ALSO ini_set('display_errors',1); error_reporting(E_ALL); session_name('s'); session_start(); session_unset(); session_destroy(); returns no errors - just a blank page. Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351146 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 As a note: This is my PHP.INI ; The path for which the cookie is valid. ; http://php.net/session.cookie-path session.cookie_path = / ; The domain for which the cookie is valid. ; http://php.net/session.cookie-domain session.cookie_domain = .mydomain.com Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351150 Share on other sites More sharing options...
hackalive Posted June 4, 2012 Author Share Posted June 4, 2012 bump Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351240 Share on other sites More sharing options...
silkfire Posted June 4, 2012 Share Posted June 4, 2012 Do you want to destroy a cookie or a session? And for what purpose? User logoff? Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351245 Share on other sites More sharing options...
hackalive Posted June 5, 2012 Author Share Posted June 5, 2012 As part of the logoff process - like FB does Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351369 Share on other sites More sharing options...
silkfire Posted June 5, 2012 Share Posted June 5, 2012 I don't know how you initiate a logon in your system, but I simply make a $_SESSION['logged_in'] = true and always check if that variable exists to see if user is online. During logoff, $_SESSION = array() will remove that variable and the user will be logged off. The session is still there but with no variables. Why would this method not work for you? Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351375 Share on other sites More sharing options...
hackalive Posted June 6, 2012 Author Share Posted June 6, 2012 I have included part of the login process above if you take a look. Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351590 Share on other sites More sharing options...
Jessica Posted June 6, 2012 Share Posted June 6, 2012 Not according to the code you've posted. Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1351677 Share on other sites More sharing options...
hackalive Posted June 12, 2012 Author Share Posted June 12, 2012 Not according to the code you've posted. Look at reply #16 - login.php (Issue STILL not resolved). Quote Link to comment https://forums.phpfreaks.com/topic/263637-correctly-destroy/#findComment-1353134 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.