Hi !,

I have some questions on session variables,

when a user logs in to a site, then opens a link in a different browser will the same session continue for the second browser ?..... i hope so... and if the session continues with the second browser and the user closes the second browser will the session come to an end but still he kept his first browser opened !... a bit confusing for me..

hope i will get a answer .. also not sure if my question is correct !...

Thanks !! :)


No, that's not the case. The session id is saved in a cookie, which is browser specific. Thus opening a new browser starts another session.

The workaround would be saving the session id in a database along with an ip. That way you can pick the session by an ip, not a cookie, thus making the same session available in different browsers on the same computer.

That is both correct and incorrect. The second page will NOT identify the session when it opens. However a session will NOT end until you tell it to end or it times out. Even if it is stored in a cookie.

The purpose of using sessions is so that your user can close the browser and come back in a new window. So it will still be preserved, this much I have delt with on the forum I built. However, the second page MUST include the Session_start() function or it will not even pay any attention to the fact that you have an active session.

yes !.. session id is stored in a cookie... but the above 2 replies seems to be completly contradict. There are cases where a session will end once the browser is closed like gmail. what will happen in that case when a link is opened in a different window?. and if i go with what php_deamon said, then will a new cookie get created if opened in a seperate window for those cases where session is not ended by closing the browser like how DG said, like... the purpose is to retain a session when a browser is closed and opend again... but if a link is opend in a different window will it create a new cookie......

There are two session types: persistent and simple sessions.

The persistent sessions are established by setting a session cookie lifespan for a specific period (for example a month). In this case, they work as DGStudios described.

However, the sessions are not persistent by default. The cookie is kept only until the browser is closed. If you close the browser, the session is no longer alive the next time you open it. As far as the new windows, it depends whether you open it by following the link or run a different instance of a browser. When you follow a link to a new window, that window is a child of the main window and shares the same cookie information. But if you run a different instance of a browser, the sessions do not persist.

commented: Informative, Knowledgeable !! +2

thats really very nice usefull piece of information by php_daemon ! Thanks !

so if i am correct... the simple sessions are created by setting the cookie date to the past on the header of a php page... right ? .... i am not exactly sure though

No, if you check the default setting for session.cookie_lifetime, it's 0.

yes... thats in the server !!.. but if the session lifetime in the server is set to infinite then using the code below you can make the session lifetime to 0.

header("Expires: Thu, 17 May 2001 10:17:17 GMT");    // Date in the past
  	header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); // always modified
	header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
	header ("Pragma: no-cache");                          // HTTP/1.0