I understand the $_SESSION
thing and I use it, but what session_id really is and what does it give? I don't get it, can you give me a hand? Thank you.
views:
69answers:
3session_id()
returns the value of the session cookie by the name returned from session_name()
. session_id()
is normally a very long hash that is unique to the client.
PHP could internally implement the setting of the client's cookie this way:
setcookie( session_name(), session_id() );
I recommend that you read the Session reference on php.net. You can get the session ID with session_id()
. It's value is generated by PHP.
Well you need a small lesson on web technology in general.
Protocol we are using, named HTTP, is stateless. Means it never keep track on requests. Even if you click several links on the site, each request would be fresh new, as though there was no previous ones. There is no way to distinguish requests from the same client.
Thus, if we want to distinguish clients, we have to "mark" them somehow. Assign some unique identifier and make them send it with each request. So your session id is that mark. When you start a session for the first time, a cookie is sent along with server's response. Good client always send all cookies back with every consecutive request. So, we can recognize that client.
Throw in a file on the server side, named after this session id, to store session data - and now you've got a session mechanism!