tags:

views:

24

answers:

3

Excerpt from http://php.about.com/od/advancedphp/ss/php_sessions.htm:

So how will it know it's me? Most sessions set a cookie on your computer to uses as a key... it will look something like this: 350401be75bbb0fafd3d912a1a1d5e54.

My question is, in PHP, how to generate a key (e.g., 350401be75bbb0fafd3d912a1a1d5e54) for a session cookie?

And when do we need such a key? Why not just set $_SESSION['color']='red' in the first page and retrieve in the second page with $_SESSION['color']?

+1  A: 

how to generate a key (e.g., 350401be75bbb0fafd3d912a1a1d5e54) for a session cookie?

Just call session_start() for this. A key would be generated automatically

when do we need such a key?

when session starts, to distinguish one user from another

Why not just set $_SESSION['color']='red' in the first page and retrieve in the second page with $_SESSION['color']?

This is the way sessions works. You are encouraged to do it this way. Who says you can't do it?

Col. Shrapnel
Also session_regenerate_id() if for some reason you need to generate a *new* key. Sounds like session_start() is all you need here, though.
It means that for most cases, we don't need to handle the key manually?
powerboy
I asked this question because I just started reading source code of a project. I noticed in the database schema, there is a `session_cookie` column in the `sessions` table and donno what it is for.
powerboy
@powerboy There are always more than one way to do something. It is better to ask the code author for the explanation. But in general one don't need not session_cookie column, nor sessions table nor manually generated key.
Col. Shrapnel
@Col - Is session information saved on the server or on the client via cookies? If it is the latter, what if cookie is disabled by the browser?
powerboy
@powerboy session information being saved in the file on the server, while name of this file is the session cookie key itself. So, the file being identified by the session cookie. So, you need cookies only to identify users, not to store variables. Though sessions may work with cookies disabled, passing session key via URL, but it is not recommended for security reasons. You'd better refer to the official man page to get the picture of how sessions works.
Col. Shrapnel
A: 

When youu start a session in PHP using session_start it auto generates a session key.

Check the session section on the PHP manual http://www.php.net/manual/en/book.session.php

Lizard
A: 
And when do we need such a key? Why not just set $_SESSION['color']='red'
in the first  page and retrieve in the second page with $_SESSION['color']?

The key's a unique identifier for each user to your site. If everyone received the same session ID, then they'd all be sharing the same session ID. Think of what'd happen if your bank's website used the same key for everyone. The first person to log in would then have their account exposed to every other visitor.

You can store whatever you want in the $_SESSION array, but remember that if things were correctly configured, it's going to be a different array for every user, so only store whatever's "configurable" per-user. A color preference for a background, like your 'red' example is one. But don't store the name of your site, as that wouldn't differ for each user.

Marc B