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
Share on other sites

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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.