Certified PHP Developer Learning Resources Session Internals

Session Internals

Sessions in PHP are started by using the session_start( ) function.  Like the setcookie( ) function, the session_start( ) function must come before any HTML, including blank lines, on the page.  It will look like this:

session_start( );

....... etc

The session_start( ) function generates a random Session Id and stores it in a cookie on the user's computer (this is the only session information that is actually stored on the client side.)  The default name for the cookie is PHPSESSID, although this can be changed in the PHP configuration files on the server (most hosting companies will leave it alone, however.)  To reference the session Id in you PHP code, you would therefore reference the variable $PHPSESSID (it's a cookie name; remember that from Cookies?)

Your sharp mind may be wondering what happens when you come to the second pass through your page and reach the session_start( ) function again.  PHP knows that there is already a session on progress and so ignores subsequent instances of the session_start( )  

 For Support