Hello all-<br /><br />I am having problems with this, and I just can't find the answer I'm looking for. So I'll ask it to the Total Choice Family.<br /><br />I wrote a program in PHP, where users log in/log out. I start a session when they log in and when they hit the 'logout' button, it destroys the session.<br /><br />My problem is that if the user closes the browser window without choosing 'logout', the session is still active! I know this because I've tested it a few times, with closing the window and trying to open up a page again and its still showing my session variables.<br /><br />I know to check for a valid login on every page, but that's not the point. I need to make sure that the session will destroy itself on close of the browser. <br /><br />Can this be accomplished by using PHP and JavaScript?<br /><br />Please help. Thanks.<br /><br />Sarah<!--content-->
I havent seen an onclose function for the session though others may have. Are you using cookies for this? You may check here <!--coloro:blue--><span style="color:blue"><!--/coloro-->http://ca3.php.net/manual/en/function.session-set-cookie-params.php<!--colorc--></span><!--/colorc--> and see if this may help.<!--content-->
I'm not using any cookies, as far as I know. All that I am using are session variables to store login data, and data about the user I retrieve from the database.<br /><br />I've checked out that link you gave me, but I'm not really sure how to use those config settings with this situation.<!--content-->
Sarah, by default, PHP stores session information in a browser cookie. If that isn't available *and* PHP is configured for this, it will then try to use a variable from the URL. When you see an URL such as <!--coloro:blue--><span style="color:blue"><!--/coloro-->http://******/index.php?PHPSESSID=a22e6a8c5dcbb91fe6384d21cead7990<!--colorc--></span><!--/colorc-->, it means PHP wasn't able to store the session information in a cookie and used the PHPSESSID variable in the URL instead.<br /><br />Now about your problem: if you don't have such a variable in the URL, it means PHP is using cookies to track your sessions. By default, that cookie should be deleted when you close the browser but for some reason, it's not getting deleted (perhaps PHP is configured differently on your server? or perhaps you changed the session.cookie_lifetime configuration directive?).<br /><br />You can try add this to your code:<br /><br />session_set_cookie_params(0);<br /><br />If something changed the default cookie lifetime, that will put it back to the default (cookie getting deleted when you close the browser).<br /><br />Other than that, I can't figure out any other way of getting the session to terminate - assuming you're using session_destroy() correctly (not much to get wrong, anyway).<!--content-->
The session ID is not being passed through the URL, so it is storing it in a cookie, apparently.<br /><br />I checked my phpinfo file, and it said that <br />session.cookie_lifetime = 0, and session.cache_expire = 180. <br />These were the defaults for the server also. I've never changed anything in the php.ini file.<!--content-->
You must close out all INSTANCES of a browser to be rid of sessions.<br /><br />Say you open up IE, then spawn a new window from it in some form or other (CTRL-N, a link, etc). Now say you visit another website in one window, and the sessioned website in the other. You close the sessioned website.<br /><br />Your session is still active because of the other window. Close all spawned instances of the browser, and the session dies.<!--content-->
Sarah, there must be something wrong with your code, then <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/sad.gif" style="vertical-align:middle" emoid="" border="0" alt="sad.gif" /><br /><br />If you paste the relevant parts here (remember to strip out usernames, passwords and other sensitive information), perhaps we can figure out what the problemis - sometimes one can miss the obvious (because of being tired, bored of coding the darn thing and not being able to get rid of that error message, etc...) but ask someone else to look at the code and that person will tell you right away where the problem is <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid="" border="0" alt="smile.gif" /><br /><br />Edit: Robert may also be right, have you closed all browser instances?<!--content-->
Well, there's alot of code to post... too much for here.... I don't know what I would have coded wrong anyways... I just create a session and destroy it on the logout screen. I just want to make sure it's closed if the user closes all instances of the browser. <br /><br />I will check on that again, checking the instances.<!--content-->
Well I just checked the instance problem. Apparently, I wasn't closing out of all browser instances. It works now.<br /><br />I guess there's not a way to just do it with the one window (which isn't too much of a problem I guess). Do I need to change any PHP config settings for the timeout anyways? What's the default timeout for a logout?<!--content-->
Dunno how to set the session timeout... If I knew, I'd shorten it for most of my projects.<!--content-->
You can accomplish this without a php.ini setting by using the function:<br /><br /> session_set_cookie_params(int lifetime [, string path [, string domain]])<br /><br />How about editing .htaccess:<br /><br />php_value session.gc_maxlifetime 72000<br />php_value session.cookie_lifetime 72000 <br /><br /><br />You can find info on customized timeout sessions at <br />http://www.phpfreaks.com/tutorials/77/6.php<br /><br />I have not tested these myself but it is something to look at. I can not verify they will work but it is a start.<!--content-->
I don't know much about .htaccess anyways. I guess i'll just leave well enough alone. I made a different timeout script that logs people out when they are inactive, so it will bypass alot of nonsense. Thanks for the help. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid="" border="0" alt="smile.gif" /><!--content-->
I havent seen an onclose function for the session though others may have. Are you using cookies for this? You may check here <!--coloro:blue--><span style="color:blue"><!--/coloro-->http://ca3.php.net/manual/en/function.session-set-cookie-params.php<!--colorc--></span><!--/colorc--> and see if this may help.<!--content-->
I'm not using any cookies, as far as I know. All that I am using are session variables to store login data, and data about the user I retrieve from the database.<br /><br />I've checked out that link you gave me, but I'm not really sure how to use those config settings with this situation.<!--content-->
Sarah, by default, PHP stores session information in a browser cookie. If that isn't available *and* PHP is configured for this, it will then try to use a variable from the URL. When you see an URL such as <!--coloro:blue--><span style="color:blue"><!--/coloro-->http://******/index.php?PHPSESSID=a22e6a8c5dcbb91fe6384d21cead7990<!--colorc--></span><!--/colorc-->, it means PHP wasn't able to store the session information in a cookie and used the PHPSESSID variable in the URL instead.<br /><br />Now about your problem: if you don't have such a variable in the URL, it means PHP is using cookies to track your sessions. By default, that cookie should be deleted when you close the browser but for some reason, it's not getting deleted (perhaps PHP is configured differently on your server? or perhaps you changed the session.cookie_lifetime configuration directive?).<br /><br />You can try add this to your code:<br /><br />session_set_cookie_params(0);<br /><br />If something changed the default cookie lifetime, that will put it back to the default (cookie getting deleted when you close the browser).<br /><br />Other than that, I can't figure out any other way of getting the session to terminate - assuming you're using session_destroy() correctly (not much to get wrong, anyway).<!--content-->
The session ID is not being passed through the URL, so it is storing it in a cookie, apparently.<br /><br />I checked my phpinfo file, and it said that <br />session.cookie_lifetime = 0, and session.cache_expire = 180. <br />These were the defaults for the server also. I've never changed anything in the php.ini file.<!--content-->
You must close out all INSTANCES of a browser to be rid of sessions.<br /><br />Say you open up IE, then spawn a new window from it in some form or other (CTRL-N, a link, etc). Now say you visit another website in one window, and the sessioned website in the other. You close the sessioned website.<br /><br />Your session is still active because of the other window. Close all spawned instances of the browser, and the session dies.<!--content-->
Sarah, there must be something wrong with your code, then <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/sad.gif" style="vertical-align:middle" emoid="" border="0" alt="sad.gif" /><br /><br />If you paste the relevant parts here (remember to strip out usernames, passwords and other sensitive information), perhaps we can figure out what the problemis - sometimes one can miss the obvious (because of being tired, bored of coding the darn thing and not being able to get rid of that error message, etc...) but ask someone else to look at the code and that person will tell you right away where the problem is <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid="" border="0" alt="smile.gif" /><br /><br />Edit: Robert may also be right, have you closed all browser instances?<!--content-->
Well, there's alot of code to post... too much for here.... I don't know what I would have coded wrong anyways... I just create a session and destroy it on the logout screen. I just want to make sure it's closed if the user closes all instances of the browser. <br /><br />I will check on that again, checking the instances.<!--content-->
Well I just checked the instance problem. Apparently, I wasn't closing out of all browser instances. It works now.<br /><br />I guess there's not a way to just do it with the one window (which isn't too much of a problem I guess). Do I need to change any PHP config settings for the timeout anyways? What's the default timeout for a logout?<!--content-->
Dunno how to set the session timeout... If I knew, I'd shorten it for most of my projects.<!--content-->
You can accomplish this without a php.ini setting by using the function:<br /><br /> session_set_cookie_params(int lifetime [, string path [, string domain]])<br /><br />How about editing .htaccess:<br /><br />php_value session.gc_maxlifetime 72000<br />php_value session.cookie_lifetime 72000 <br /><br /><br />You can find info on customized timeout sessions at <br />http://www.phpfreaks.com/tutorials/77/6.php<br /><br />I have not tested these myself but it is something to look at. I can not verify they will work but it is a start.<!--content-->
I don't know much about .htaccess anyways. I guess i'll just leave well enough alone. I made a different timeout script that logs people out when they are inactive, so it will bypass alot of nonsense. Thanks for the help. <img src="http://www.totalchoicehosting.com/forums/style_emoticons/default/smile.gif" style="vertical-align:middle" emoid="" border="0" alt="smile.gif" /><!--content-->