session_name()
session_name() — Get and/or set the current session name.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
session_name() returns the name of the current session. If name is given, session_name() will update the session name and return the old session name.
Signature
session_name(?string $name = null): string|false
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $name | ?string | The session name references the name of the session, which is used in cookies and URLs (e.g. PHPSESSID). It should contain only alphanumeric characters; it should be short and descriptive (i.e. for users with enabled cookie warnings). If name is specified and not null, the name of the current session is changed to its value. |
Return value
Returns the name of the current session. If name is given and function updates the session name, name of the old session is returned, or false on failure.
Example
A runnable snippet:
/* set the session name to WebsiteID */ $previous_name = session_name("WebsiteID"); echo "The previous session name was $previous_name<br />";
Changelog
| Version | Change |
|---|---|
| 8.0.0 | name is nullable now. |
| 7.2.0 | session_name() checks session status, previously it only checked cookie status. Therefore, older session_name() allows to call session_name() after session_start() which may crash PHP and may result in misbehaviors. |
All PHP 8.5 built-in functions
Reference card built from the PHP manual page session_name() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.