PHP Session

In this lesson, you will learn about PHP sessions, their usage, and examples to better understand the topic.


Why use sessions and cookies?

Websites use sessions and cookies to save user data across various internet pages. Both sessions and cookies are crucial since they record the data supplied by a visitor for multiple purposes. Sessions are held on the server; in contrast, cookies are saved on the user’s browser or client side, which is the primary distinction between the two.

There are numerous more differences between the two in addition to this. We shall comprehend a thorough explanation of sessions and cookies in this tutorial and how they differ from one another.


PHP Session

A session temporarily saves the server’s data and uses it on several website pages. The user’s session begins when he signs into a specific network application and ends when he exits the application or shuts down the computer.

Since the HTTP protocol doesn’t keep track of the state, when working on an application online, the webserver is unaware of the user. The user’s data won’t be transmitted to another application page from one page (let’s say, Homepage). Sessions are utilized to get rid of this restriction. A session is initiated when a user accesses a website for the first time.

Session variables, which can hold any value or data type of an Object, are where the user information is kept. Session values are highly secure because they are maintained in encrypted or binary form. These session values will only be decrypted on the server. The session values are automatically deleted when users shut down their computers or log out of an application. We must keep the values in the database to store them permanently. Each session is distinct for each user, and there is no restriction on the number of sessions a user uses in an application.

SessionID, a one-of-a-kind number stored within the server, is used to identify the user. It is kept as a cookie, form field, or URL.


Why does PHP use the session?

Sessions are utilized to securely store information such as UserID on the server, where it cannot be altered. It can also transfer data from one web page to another as a value. A session is a substitute for cookies to store variables more securely on browsers that do not support cookies.


PHP Create session

The session start() function initiates a session. The PHP global variable $_SESSION is used to set session variables. Create a new page called mysession.php. We create a new PHP session and set a few session variables on this page, and use session unset()and session destroy() to delete all global session variables and end the current session.

The next step is to construct mysession.php.We can access the session data specified on the first page (“mysession1.php”) from this one. You’ll notice those session variables aren’t sent to new pages separately. Instead, they are retrieved from the session we initiate at the beginning of each page (session start()). Also, note that the global $_SESSION variable contains the values of all session variables:

Example

<?php
// Start the session
session_start(); ?>
<!DOCTYPE HTML>
<HTML>
<body>

<?php
// creates the session variable
$_SESSION["name"] = "Yahya";
//accessing the session variable
echo $_SESSION["name"] . "\n";
echo "This is my session variable.";
// destroying the session
session_unset();
session_destroy();
?>

</body>
</HTML>

This concludes the PHP Session lesson. In the next lesson, you will learn about Cookies in PHP.