PHP Sessions
A session is a temporary place to store user data on server side.
A session will be unique for every user. Seesion allows every user to store their data on it.
The user data will be stored in the form of variables in the server.
A session is very useful to transfer the data between web pages.
Note:- The data is not saved on the user's computer, as opposed to a cookie.
What is a PHP Session
Data transfer and keep track of data must happen in anyway in all applications.
In a web application data tranfer is happening by HTTP protocol.
The HTTP protocol is a stateless protocol and can store only one value or request (current requested page) at a time.
So it cannot store previous request information and making data transfer between webpage.
Session variables are a way to solve this problem by saving user data that can be used across different pages (e.g. username, favourite color, etc). Session variables are set to last till the user quits the browser.
Note:- As a result, session variables store information about a single user and are accessible from every page in a website.
Tip :- If you require long-term storage, you may want to consider storing the information in a database.
Start a PHP Session
The session_start()
method is used to initiate a session.
$_SESSION
is a global variable that controls or make accessible session variables between pages.
Example :- Let's now start the "demo-session1.php" new page. We establish a new PHP session on this page and specify certain session variables.
Output :-
Note:- The session_start()
function must occur before any html tag, or before
sending any text to the browser screen.
Get PHP Session Variable Values
The next page we'll develop is "demo-session2.php". We get the information from the session that we put into the first page from this page ("demo-session1.php").
Session variables are not supplied separately to each new page; instead, they are taken from the session
we open at the start of each page (session_start()
).
Example 1 :- It's also worth noting that the global $_SESSION
variable stores the
values of all session variables created by us.
";
echo "Age is " . $_SESSION["age"] . ".";
?>
Output :-
Age is 25.
Example 2:- Run the following code to display all of the session variables and values for a user session.
Output :-
How does it work? How does it know it's me :
When a session is opened on a different website, it searches the computer for a user-key 765487cf34ert8dede5a562e4f3a7e12. If there is a match, it accesses that session; otherwise, it begins a new one.
Related Links
Modify a PHP Session Variable
Simply overwrite a session variable to change the value. You can alter the value from any pages.
Example :-
Output :-
Destroy a PHP Session
The session_unset()
and session_destroy()
functions are used to delete or destroy
all session variables for current/specific user.
A session will destroy automatically in some situations:
- When user close the browser window or tab
- When user moves to a different website from current website not pages (from simmanchith.com to microsoft.com).
Example :-
Output :-
Related Links