rv20 Posted May 24, 2009 Share Posted May 24, 2009 So i created a script with, //test1.php session_start(); $_SESSION['user'] = "joe2"; then another script and ran it independently of the first one //test2.php echo $_SESSION['user']; i thought that test2.php would have the session data from test1.php so how do sessions work, also are they erased as soon as the brower closes? Link to comment https://forums.phpfreaks.com/topic/159422-how-do-sessions-work/ Share on other sites More sharing options...
jackpf Posted May 24, 2009 Share Posted May 24, 2009 Sessions are stored on the server. A cookies stores a unique ID pointing to a session. The session expires whenever you set it to, in php.ini or at runtime. The cookie, however, generally is deleted when the browser is closed, although you can change that too. Link to comment https://forums.phpfreaks.com/topic/159422-how-do-sessions-work/#findComment-840958 Share on other sites More sharing options...
hitman6003 Posted May 24, 2009 Share Posted May 24, 2009 Sessions use a cookie (by default) to store a session id, which the server then uses to reference data stored for the identified session (stored in the server's temp directory by default). The cookie is lost when the browser is closed, which means that it will no longer be passed back to the server on subsequent visits, and the data is no longer associated with the user. The data is lost after a configurable period of time, I seem to recall that 60 minutes is the default. When you say you "ran it independently of the first one", were you using the CLI or a browser? Sessions do not apply to CLI scripts. Link to comment https://forums.phpfreaks.com/topic/159422-how-do-sessions-work/#findComment-840960 Share on other sites More sharing options...
rv20 Posted May 24, 2009 Author Share Posted May 24, 2009 Sessions use a cookie (by default) to store a session id, which the server then uses to reference data stored for the identified session (stored in the server's temp directory by default). The cookie is lost when the browser is closed, which means that it will no longer be passed back to the server on subsequent visits, and the data is no longer associated with the user. The data is lost after a configurable period of time, I seem to recall that 60 minutes is the default. When you say you "ran it independently of the first one", were you using the CLI or a browser? Sessions do not apply to CLI scripts. Just the browser, i just opened a new brower then 127.0.0.1/test2.php etc....hmm so bit confused about sessions Vs cookies, am i right in saying both are actually cookies but session cookies are stored on the server side whereas setcookie cookies are stored client side? Can i just completely forget about sessions and use setcookie instead, seems as though setcookie is the better option?? Link to comment https://forums.phpfreaks.com/topic/159422-how-do-sessions-work/#findComment-840968 Share on other sites More sharing options...
hitman6003 Posted May 24, 2009 Share Posted May 24, 2009 Cookies store data only on the client side, every page visit the data is sent to the server. Sessions store data on the server. A client cookie is used to identify which user is accessing the page in order to determine which session data file to read. The actual session data is not stored in a cookie, only the session identifier. However, with sessions the cookie is optional. When the cookie behavior is turned off the session identifier will be passed to the server in the url (you most often see this in search engine results, when the url has something like "sessionid=......" in it) Link to comment https://forums.phpfreaks.com/topic/159422-how-do-sessions-work/#findComment-840970 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.