PHP PHP session_name with example: set & get session name

PHP session_name with example: set & get session name

Session name in PHP at

Session name in PHP is usually a random name provided by server which is unique for any web app. We will discuss below , how to get and set session name with PHP session_name example.

Introduction to session and session_name

Sessions are an easy way to store information for individual users against a unique session ID. This can be used to carry on state information between page requests. Session IDs are usually sent to the browser via session cookies and session ID is used to retrieve existing session information.

If a session ID or session cookie is not present then server sends/creates a new session and generates a new session ID on browser.

In PHP, session names can be set or retrieve by function session_name()

To set a session name we can pass any string argument as a parameter to function session_name(). The argument passed can be any string, but not numeric or blank.

AS per my analysis the valid characters for a session name, are mentioned below, but I would recommend keeping the session name simple and readable. One more thing is, if you need to include single or double quotes in session name (not at all recommended) use a backslash before it as session name is itself a string. I will be showing in example below

~ ! @ # $ % ^ & * ( ) _ + = – ` ] [ } { | \ ‘ ” ; : / . , ? > <

PHP session_name Examples:

To get a session name we need to call function session_name() without passing any argument. This will return the session name that is being set.
IF there is no specific session name set, then in that case a string “PHPSESSID” will be returned as session name which is a default value for session name.

Leave a Reply