PHP Sessions

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.

You can also search for these topics, session by php, how sessions will work, php session, working of sessions in php, what is the php sessions, use of php sessions.

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.

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>
<?php
// Set session variables
$_SESSION["name"] = "abcd";
$_SESSION["age"] = "25";
echo "Session variables are set.";
?>
</body>
</html>

Output :-

Session variables are set.

Note:- The session_start() function must occur before any html tag, or before sending any text to the browser screen.

You can also search for these topics, start a php session, how to start a php session, php session_start() function, start a php session example.

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.

<?php
session_start();
// Echo session variables that were set on previous page
echo "Name is " . $_SESSION["name"] . ".<br />";
echo "Age is " . $_SESSION["age"] . ".";
?>

Output :-

Name is abcd.
Age is 25.

Example 2:- Run the following code to display all of the session variables and values for a user session.

<?php
session_start();
print_r($_SESSION);
?>

Output :-

Array ( [name] => abcd, [age] => 25 )

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.



You can also search for these topics, how to get session variable values, create a php session variable values, working of get php session variable values, Example for get php session variable values.

Modify a PHP Session Variable

Simply overwrite a session variable to change the value. You can alter the value from any pages.

Example :-

<?php
session_start();
// to change a session variable, just overwrite it
$_SESSION["age"] = "33";
echo "New Age is : " . $_SESSION["age"];
?>

Output :-

New Age is : 33

You can also search for these topics, php modify a session variable, session variable to modify using php, alter a session variable in php, how to change a session variable in php, example for modify a session variable in php.

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 :-

<?php
session_start();
// remove all session variables
session_unset();
// destroy the session
session_destroy();
echo "All session variables are now removed, and the session is destroyed."
?>

Output :-

All session variables are now removed, and the session is destroyed.



You can also search for these topics, php delete a session variable, session variable to delete using php, destroy a session variable in php, how to remove a session variable in php, example for delete a session in php, php session_unset() function.