Session handling functions Sessions
&reftitle.intro; Session support in PHP consists of a way to preserve certain data across subsequent accesses. This enables you to build more customized applications and increase the appeal of your web site. If you are familiar with the session management of PHPLIB, you will notice that some concepts are similar to PHP's session support. A visitor accessing your web site is assigned an unique id, the so-called session id. This is either stored in a cookie on the user side or is propagated in the URL. The session support allows you to register arbitrary numbers of variables to be preserved across requests. When a visitor accesses your site, PHP will check automatically (if session.auto_start is set to 1) or on your request (explicitly through session_start or implicitly through session_register) whether a specific session id has been sent with the request. If this is the case, the prior saved environment is recreated. All registered variables are serialized after the request finishes. Registered variables which are undefined are marked as being not defined. On subsequent accesses, these are not defined by the session module unless the user defines them later. Session handling was added in PHP 4.0.
Sessions and security Using sessions, does not mean, you can be absolutely sure, that the session data can only be viewed by that user. This is important to keep in mind, when storing and displaying sensitive information. When storing data into a session, one should always ask themselves, what the damage is, when somebody else views that information, or how your application is affected when this session is actually somebody else. For instance, if somebody else takes a session, can he then post a message in a forum, as that user and how big of a problem is that? Or perhaps he can view what the original user was thinking of ordering, because he gets access to that user's shopping cart. Obviously for a flowershop, this is less dramatic, than for a pharmacy. Therefore, when dealing with sensitive information, there should always be additional methods to decide whether it is a valid session. Sessions are not reliable as a secure authentication mechanism. Sessions rely on the session ID, meaning one can 'steal' a session, by stealing the session ID. This can be made harder, by using a cookie specifically a session cookie, but does not in any way make it impossible and still relies on the user closing all browser windows, to expire the session cookie. Besides that, even session cookies can be sniffed on a network or logged by a proxyserver.
&reftitle.required; &no.requirement;
&reftitle.install; Session support is enabled in PHP by default. If you would not like to build your PHP with session support, you should specify the option to configure.
&reference.session.ini;
&reftitle.resources; &no.resource;
&reference.session.constants;
&reftitle.examples; As of PHP 4.1.0, $_SESSION is available as global variable just like $_POST, $_GET, $_REQUEST and so on. Unlike $HTTP_SESSION_VARS, $_SESSION is always global. Therefore, you do not need to use the global keyword for $_SESSION. Please note that this documentation has been changed to use $_SESSION everywhere. You can substitute $HTTP_SESSION_VARS for $_SESSION, if you prefer the former. The keys in the $_SESSION associative array are subject to the same limitations as regular variable names in PHP, i.e. they cannot start with a number and must start with a letter or underscore. For more details see the section on variables in this manual. If register_globals is disabled, only members of the global associative array $_SESSION can be registered as session variables. The restored session variables will only be available in the array $_SESSION. Use of $_SESSION (or $HTTP_SESSION_VARS with PHP 4.0.6 or less) is recommended for improved security and code readablity. With $_SESSION, there is no need to use the session_register, session_unregister, session_is_registered functions. Session variables are accessible like any other variables. Registering a variable with $_SESSION. ]]> Unregistering a variable with $_SESSION and register_globals disabled. ]]> Unregistering a variable with register_globals enabled, after registering it using $_SESSION. ]]> If register_globals is enabled, then each global variable can be registered as session variable. Upon a restart of a session, these variables will be restored to corresponding global variables. Since PHP must know which global variables are registered as session variables, users need to register variables with session_register function. You can avoid this by simply setting entries in $_SESSION. If you are using $_SESSION and disable register_globals, do not use session_register, session_is_registered and session_unregister, if your scripts shall work in PHP 4.2 and earlier. You can use these functions in 4.3 and later. If you enable register_globals, session_unregister should be used since session variables are registered as global variables when session data is deserialized. Disabling register_globals is recommended for both security and performance reasons. Registering a variable with <link linkend="ini.register-globals"><literal>register_globals</literal></link> enabled ]]> If register_globals is enabled, then the global variables and the $_SESSION entries will automatically reference the same value for session variables which were registered in prior session instances. Additionally, if you register a new session variable by using session_register, the entry in the global scope and the $_SESSION entry will not reference the same value until the next session_start (this applies to PHP 4.2 and before only). I.e. a modification to the global variable will not be reflected by the $_SESSION entry. This is unlikely to matter in practice and has been corrected in PHP 4.3.
Passing the Session ID There are two methods to propagate a session id: Cookies URL parameter The session module supports both methods. Cookies are optimal, but since they are not reliable (clients are not bound to accept them), we cannot rely on them. The second method embeds the session id directly into URLs. PHP is capable of doing this transparently if compiled with --enable-trans-sid. This option is always enabled in PHP 4.2 and later. If you enable this option, relative URIs will be changed to contain the session id automatically. Alternatively, you can use the constant SID which is defined, if the client did not send the appropriate cookie. SID is either of the form session_name=session_id or is an empty string. The arg_separator.output &php.ini; directive allows to customize the argument seperator. The following example demonstrates how to register a variable, and how to link correctly to another page using SID. Counting the number of hits of a single user Hello visitor, you have seen this page times.

; ( can be used if short tag is enabled) # is necessary to preserve the session id # in the case that the user has disabled cookies ?> To continue, click here ]]> The <?=SID?> is not necessary, if --enable-trans-sid was used to compile PHP. Non-relative URLs are assumed to point to external sites and hence don't append the SID, as it would be a security risk to leak the SID to a different server.

Custom Session Handlers To implement database storage, or any other storage method, you will need to use session_set_save_handler to create a set of user-level storage functions.
&reference.session.functions;