HomePHP Page 4 - Introduction to Maintaining the State of Applications with PHP Sessions
More session functions ahead: finding out the name of active sessions - PHP
In PHP, session management is used to help web applications maintain their state across several HTTP requests when needed. In this first part of a series, you will learn the basics of the PHP built-in session mechanism, as well as some of its many useful functions.
Just in case you didn’t know, the PHP session module offers many other helpful functions that can be used for obtaining the values of specific parameters, which are included within the set of directives of the php.ini configuration file. However, let me first show you another illustrative example, which displays the name of an active session:
session_start(); echo session_name(); // displays PHPSESSID // generate new session name session_name('NEW_SESSION_NAME'); //displays NEW_SESSION_NAME echo session_name();
As shown in the above script, the name of a session can be obtained by using the “session_name()” function. Very similar to the “session_id()” function that I described a few lines above, this function returns the name of the active session or assigns a new name to it.
After running the previous snippet, the output I get on my browser is the following:
PHPSESSID NEW_SESSION_NAME
If a new session is started with the same browser or even with a different one, the value returned by the “session_name()” function (without passing any argument to it) will always be “PHPSESSID,” because this is the default session name assigned in the php.ini configuration file.
Right, as you saw, the PHP session module exposes many interesting functions, either for retrieving or manipulating some parameters assigned as default values within the php.ini file. Keeping in mind this concept, join me in the next section to learn how these values can be used (or eventually changed) within your PHP scripts.