Jump to content

How do sessions work


rv20

Recommended Posts

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.