ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://www.php.net/manual/en/session.examples.basic.php |
| Last Crawled | 2026-04-15 05:55:41 (12 hours ago) |
| First Indexed | 2019-04-23 03:39:12 (6 years ago) |
| HTTP Status Code | 200 |
| Meta Title | PHP: Basic usage - Manual |
| Meta Description | Basic usage |
| Meta Canonical | null |
| Boilerpipe Text | Sessions are a simple way to store data for individual users against a unique session ID.
This can be used to persist state information between page requests. Session IDs are normally
sent to the browser via session cookies and the ID is used to retrieve existing session data.
The absence of an ID or session cookie lets PHP know to create a new session, and generate a new
session ID.
Sessions follow a simple workflow. When a session is started, PHP will either retrieve an
existing session using the ID passed (usually from a session cookie) or if no session is passed
it will create a new session. PHP will populate the
$_SESSION
superglobal
with any session data after the session has started. When PHP shuts down, it will automatically
take the contents of the
$_SESSION
superglobal, serialize it, and send it
for storage using the session save handler.
By default, PHP uses the internal
files
save handler which
is set by
session.save_handler
.
This saves session data on the server at the location specified by the
session.save_path
configuration directive.
Sessions can be started manually using the
session_start()
function.
If the
session.auto_start
directive is set
to
1
, a session will automatically start on request startup.
Sessions normally shutdown automatically when PHP is finished executing a script, but can be
manually shutdown using the
session_write_close()
function.
Example #1
Registering a variable with
$_SESSION
.
<?php
session_start
();
if (!isset(
$_SESSION
[
'count'
])) {
$_SESSION
[
'count'
] =
0
;
} else {
$_SESSION
[
'count'
]++;
}
?>
Example #2
Unregistering a variable with
$_SESSION
.
<?php
session_start
();
unset(
$_SESSION
[
'count'
]);
?>
Caution
Do NOT unset the whole
$_SESSION
with
unset($_SESSION)
as this will disable the
registering of session variables through the
$_SESSION
superglobal.
Warning
You can't use references in session variables as there is no feasible way
to restore a reference to another variable.
Note
:
File based sessions (the default in PHP) lock the session file once a
session is opened via
session_start()
or implicitly via
session.auto_start
. Once
locked, no other script can access the same session file until it has been
closed by the first script terminating or calling
session_write_close()
.
This is most likely to be an issue on Web sites that use AJAX heavily and
have multiple concurrent requests. The easiest way to deal with it is to
call
session_write_close()
as soon as any required
changes to the session have been made, preferably early in the script.
Alternatively, a different session backend that does support concurrency
could be used.
There are no user contributed notes for this page. |
| Markdown | [update page now](https://www.php.net/x-myracloud-5958a2bbbed300a9b9ac631223924e0b/1776232542.042)
[](https://www.php.net/)
- [Downloads](https://www.php.net/downloads.php)
- [Documentation](https://www.php.net/docs.php)
- [Get Involved](https://www.php.net/get-involved.php)
- [Help](https://www.php.net/support.php)
- [](https://www.php.net/releases/8.5/index.php)
Search docs
[PHP 8.4.20 Released\!](https://www.php.net/index.php#2026-04-09-2)
[Getting Started](https://www.php.net/manual/en/getting-started.php)
[Introduction](https://www.php.net/manual/en/introduction.php)
[A simple tutorial](https://www.php.net/manual/en/tutorial.php)
[Language Reference](https://www.php.net/manual/en/langref.php)
[Basic syntax](https://www.php.net/manual/en/language.basic-syntax.php)
[Types](https://www.php.net/manual/en/language.types.php)
[Variables](https://www.php.net/manual/en/language.variables.php)
[Constants](https://www.php.net/manual/en/language.constants.php)
[Expressions](https://www.php.net/manual/en/language.expressions.php)
[Operators](https://www.php.net/manual/en/language.operators.php)
[Control Structures](https://www.php.net/manual/en/language.control-structures.php)
[Functions](https://www.php.net/manual/en/language.functions.php)
[Classes and Objects](https://www.php.net/manual/en/language.oop5.php)
[Namespaces](https://www.php.net/manual/en/language.namespaces.php)
[Enumerations](https://www.php.net/manual/en/language.enumerations.php)
[Errors](https://www.php.net/manual/en/language.errors.php)
[Exceptions](https://www.php.net/manual/en/language.exceptions.php)
[Fibers](https://www.php.net/manual/en/language.fibers.php)
[Generators](https://www.php.net/manual/en/language.generators.php)
[Attributes](https://www.php.net/manual/en/language.attributes.php)
[References Explained](https://www.php.net/manual/en/language.references.php)
[Predefined Variables](https://www.php.net/manual/en/reserved.variables.php)
[Predefined Exceptions](https://www.php.net/manual/en/reserved.exceptions.php)
[Predefined Interfaces and Classes](https://www.php.net/manual/en/reserved.interfaces.php)
[Predefined Attributes](https://www.php.net/manual/en/reserved.attributes.php)
[Context options and parameters](https://www.php.net/manual/en/context.php)
[Supported Protocols and Wrappers](https://www.php.net/manual/en/wrappers.php)
[Security](https://www.php.net/manual/en/security.php)
[Introduction](https://www.php.net/manual/en/security.intro.php)
[General considerations](https://www.php.net/manual/en/security.general.php)
[Installed as CGI binary](https://www.php.net/manual/en/security.cgi-bin.php)
[Installed as an Apache module](https://www.php.net/manual/en/security.apache.php)
[Session Security](https://www.php.net/manual/en/security.sessions.php)
[Filesystem Security](https://www.php.net/manual/en/security.filesystem.php)
[Database Security](https://www.php.net/manual/en/security.database.php)
[Error Reporting](https://www.php.net/manual/en/security.errors.php)
[User Submitted Data](https://www.php.net/manual/en/security.variables.php)
[Hiding PHP](https://www.php.net/manual/en/security.hiding.php)
[Keeping Current](https://www.php.net/manual/en/security.current.php)
[Features](https://www.php.net/manual/en/features.php)
[HTTP authentication with PHP](https://www.php.net/manual/en/features.http-auth.php)
[Cookies](https://www.php.net/manual/en/features.cookies.php)
[Sessions](https://www.php.net/manual/en/features.sessions.php)
[Handling file uploads](https://www.php.net/manual/en/features.file-upload.php)
[Using remote files](https://www.php.net/manual/en/features.remote-files.php)
[Connection handling](https://www.php.net/manual/en/features.connection-handling.php)
[Persistent Database Connections](https://www.php.net/manual/en/features.persistent-connections.php)
[Command line usage](https://www.php.net/manual/en/features.commandline.php)
[Garbage Collection](https://www.php.net/manual/en/features.gc.php)
[DTrace Dynamic Tracing](https://www.php.net/manual/en/features.dtrace.php)
[Function Reference](https://www.php.net/manual/en/funcref.php)
[Affecting PHP's Behaviour](https://www.php.net/manual/en/refs.basic.php.php)
[Audio Formats Manipulation](https://www.php.net/manual/en/refs.utilspec.audio.php)
[Authentication Services](https://www.php.net/manual/en/refs.remote.auth.php)
[Command Line Specific Extensions](https://www.php.net/manual/en/refs.utilspec.cmdline.php)
[Compression and Archive Extensions](https://www.php.net/manual/en/refs.compression.php)
[Cryptography Extensions](https://www.php.net/manual/en/refs.crypto.php)
[Database Extensions](https://www.php.net/manual/en/refs.database.php)
[Date and Time Related Extensions](https://www.php.net/manual/en/refs.calendar.php)
[File System Related Extensions](https://www.php.net/manual/en/refs.fileprocess.file.php)
[Human Language and Character Encoding Support](https://www.php.net/manual/en/refs.international.php)
[Image Processing and Generation](https://www.php.net/manual/en/refs.utilspec.image.php)
[Mail Related Extensions](https://www.php.net/manual/en/refs.remote.mail.php)
[Mathematical Extensions](https://www.php.net/manual/en/refs.math.php)
[Non-Text MIME Output](https://www.php.net/manual/en/refs.utilspec.nontext.php)
[Process Control Extensions](https://www.php.net/manual/en/refs.fileprocess.process.php)
[Other Basic Extensions](https://www.php.net/manual/en/refs.basic.other.php)
[Other Services](https://www.php.net/manual/en/refs.remote.other.php)
[Search Engine Extensions](https://www.php.net/manual/en/refs.search.php)
[Server Specific Extensions](https://www.php.net/manual/en/refs.utilspec.server.php)
[Session Extensions](https://www.php.net/manual/en/refs.basic.session.php)
[Text Processing](https://www.php.net/manual/en/refs.basic.text.php)
[Variable and Type Related Extensions](https://www.php.net/manual/en/refs.basic.vartype.php)
[Web Services](https://www.php.net/manual/en/refs.webservice.php)
[Windows Only Extensions](https://www.php.net/manual/en/refs.utilspec.windows.php)
[XML Manipulation](https://www.php.net/manual/en/refs.xml.php)
[GUI Extensions](https://www.php.net/manual/en/refs.ui.php)
Keyboard Shortcuts
?
This help
j
Next menu item
k
Previous menu item
g p
Previous man page
g n
Next man page
G
Scroll to bottom
g g
Scroll to top
g h
Goto homepage
g s
Goto search
(current page)
/
Focus search box
[Passing the Session ID »](https://www.php.net/manual/en/session.idpassing.php)
[« Examples](https://www.php.net/manual/en/session.examples.php)
- [Preface](https://www.php.net/manual/en/index.php)
- [Function Reference](https://www.php.net/manual/en/funcref.php)
- [Session Extensions](https://www.php.net/manual/en/refs.basic.session.php)
- [Sessions](https://www.php.net/manual/en/book.session.php)
- [Examples](https://www.php.net/manual/en/session.examples.php)
## Basic usage
Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data. The absence of an ID or session cookie lets PHP know to create a new session, and generate a new session ID.
Sessions follow a simple workflow. When a session is started, PHP will either retrieve an existing session using the ID passed (usually from a session cookie) or if no session is passed it will create a new session. PHP will populate the [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php) superglobal with any session data after the session has started. When PHP shuts down, it will automatically take the contents of the [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php) superglobal, serialize it, and send it for storage using the session save handler.
By default, PHP uses the internal `files` save handler which is set by [session.save\_handler](https://www.php.net/manual/en/session.configuration.php#ini.session.save-handler). This saves session data on the server at the location specified by the [session.save\_path](https://www.php.net/manual/en/session.configuration.php#ini.session.save-path) configuration directive.
Sessions can be started manually using the [session\_start()](https://www.php.net/manual/en/function.session-start.php) function. If the [session.auto\_start](https://www.php.net/manual/en/session.configuration.php#ini.session.auto-start) directive is set to `1`, a session will automatically start on request startup.
Sessions normally shutdown automatically when PHP is finished executing a script, but can be manually shutdown using the [session\_write\_close()](https://www.php.net/manual/en/function.session-write-close.php) function.
**Example \#1 Registering a variable with [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php).**
```
<?php
session_start();
if (!isset($_SESSION['count'])) {
$_SESSION['count'] = 0;
} else {
$_SESSION['count']++;
}
?>
```
**Example \#2 Unregistering a variable with [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php).**
```
<?php
session_start();
unset($_SESSION['count']);
?>
```
**Caution**
Do NOT unset the whole [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php) with `unset($_SESSION)` as this will disable the registering of session variables through the [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php) superglobal.
**Warning**
You can't use references in session variables as there is no feasible way to restore a reference to another variable.
> **Note**:
>
> File based sessions (the default in PHP) lock the session file once a session is opened via [session\_start()](https://www.php.net/manual/en/function.session-start.php) or implicitly via [session.auto\_start](https://www.php.net/manual/en/session.configuration.php#ini.session.auto-start). Once locked, no other script can access the same session file until it has been closed by the first script terminating or calling [session\_write\_close()](https://www.php.net/manual/en/function.session-write-close.php).
>
> This is most likely to be an issue on Web sites that use AJAX heavily and have multiple concurrent requests. The easiest way to deal with it is to call [session\_write\_close()](https://www.php.net/manual/en/function.session-write-close.php) as soon as any required changes to the session have been made, preferably early in the script. Alternatively, a different session backend that does support concurrency could be used.
### Found A Problem?
[Learn How To Improve This Page](https://github.com/php/doc-base/blob/master/README.md "This will take you to our contribution guidelines on GitHub") • [Submit a Pull Request](https://github.com/php/doc-en/blob/master/reference/session/examples.xml) • [Report a Bug](https://github.com/php/doc-en/issues/new?body=From%20manual%20page:%20https:%2F%2Fphp.net%2Fsession.examples.basic%0A%0A---)
[+add a note](https://www.php.net/manual/add-note.php?sect=session.examples.basic&repo=en&redirect=https://www.php.net/manual/en/session.examples.basic.php)
### User Contributed Notes
There are no user contributed notes for this page.
- [Examples](https://www.php.net/manual/en/session.examples.php)
- [Basic usage](https://www.php.net/manual/en/session.examples.basic.php "Basic usage")
- [Passing the Session ID](https://www.php.net/manual/en/session.idpassing.php "Passing the Session ID")
- [Custom Session Handlers](https://www.php.net/manual/en/session.customhandler.php "Custom Session Handlers")
- [Copyright © 2001-2026 The PHP Documentation Group](https://www.php.net/manual/en/copyright.php)
- [My PHP.net](https://www.php.net/my.php)
- [Contact](https://www.php.net/contact.php)
- [Other PHP.net sites](https://www.php.net/sites.php)
- [Privacy policy](https://www.php.net/privacy.php)
[]()
`↑` and `↓` to navigate • `Enter` to select • `Esc` to close • `/` to open
Press `Enter` without selection to search using Google |
| Readable Markdown | Sessions are a simple way to store data for individual users against a unique session ID. This can be used to persist state information between page requests. Session IDs are normally sent to the browser via session cookies and the ID is used to retrieve existing session data. The absence of an ID or session cookie lets PHP know to create a new session, and generate a new session ID.
Sessions follow a simple workflow. When a session is started, PHP will either retrieve an existing session using the ID passed (usually from a session cookie) or if no session is passed it will create a new session. PHP will populate the [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php) superglobal with any session data after the session has started. When PHP shuts down, it will automatically take the contents of the [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php) superglobal, serialize it, and send it for storage using the session save handler.
By default, PHP uses the internal `files` save handler which is set by [session.save\_handler](https://www.php.net/manual/en/session.configuration.php#ini.session.save-handler). This saves session data on the server at the location specified by the [session.save\_path](https://www.php.net/manual/en/session.configuration.php#ini.session.save-path) configuration directive.
Sessions can be started manually using the [session\_start()](https://www.php.net/manual/en/function.session-start.php) function. If the [session.auto\_start](https://www.php.net/manual/en/session.configuration.php#ini.session.auto-start) directive is set to `1`, a session will automatically start on request startup.
Sessions normally shutdown automatically when PHP is finished executing a script, but can be manually shutdown using the [session\_write\_close()](https://www.php.net/manual/en/function.session-write-close.php) function.
**Example \#1 Registering a variable with [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php).**
**Example \#2 Unregistering a variable with [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php).**
**Caution**
Do NOT unset the whole [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php) with `unset($_SESSION)` as this will disable the registering of session variables through the [\$\_SESSION](https://www.php.net/manual/en/reserved.variables.session.php) superglobal.
**Warning**
You can't use references in session variables as there is no feasible way to restore a reference to another variable.
> **Note**:
>
> File based sessions (the default in PHP) lock the session file once a session is opened via [session\_start()](https://www.php.net/manual/en/function.session-start.php) or implicitly via [session.auto\_start](https://www.php.net/manual/en/session.configuration.php#ini.session.auto-start). Once locked, no other script can access the same session file until it has been closed by the first script terminating or calling [session\_write\_close()](https://www.php.net/manual/en/function.session-write-close.php).
>
> This is most likely to be an issue on Web sites that use AJAX heavily and have multiple concurrent requests. The easiest way to deal with it is to call [session\_write\_close()](https://www.php.net/manual/en/function.session-write-close.php) as soon as any required changes to the session have been made, preferably early in the script. Alternatively, a different session backend that does support concurrency could be used.
There are no user contributed notes for this page. |
| Shard | 91 (laksa) |
| Root Hash | 2869005436169329891 |
| Unparsed URL | net,php!www,/manual/en/session.examples.basic.php s443 |