Introduction
Session support in PHP consists of a way to preserve certain data
across subsequent accesses.
A visitor accessing your web site is assigned a 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 store data between requests in the
$_SESSION superglobal array. 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()) whether a specific session
id has been sent with the request. If this is the case, the prior
saved environment is recreated.
$_SESSION (and all registered variables) are serialized
internally by PHP using the serialization handler specified by the
session.serialize_handler ini setting,
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.
Warning
Because session data is serialized, resource variables cannot
be stored in the session.
The php and php_binary
serialize handlers place restrictions on
root-level keys in
$_SESSION. Nested keys inside arrays or objects
stored in the session are not subject to these restrictions.
The following root-level keys are not allowed:
-
Numeric keys (for example $_SESSION[0]).
Such keys are skipped when the session is written, and a warning
is emitted.
-
String keys that contain | when using the
php serialize handler. Using such a key causes
session write failure (and a warning as of PHP 8.5.0).
The php_serialize handler does not have these
limitations, and may be used when numeric keys or keys containing
| must be stored at the root of
$_SESSION.
Note:
Please note when working with sessions that a record of a session
is not created until a variable has been registered by adding a new
key to the $_SESSION superglobal array. This
holds true regardless of if a session has been started using the
session_start() function.