PHP Session has a very simple concept. For example, when you login to your email account, your session starts and your session expires as soon as you logout. But the use of sessions is not the same. Many times a user’s session is also controlled from time to time.
When you are filling out a government exam form If you fill the form halfway for a while, you will get a message show that your php session has expired.
If you do not fill the form in a time limit, your session expires and you have to fill the form from the beginning. Here, sessions have been used to reduce the load of the website.
Sessions is a used and happens. When a user is surfing your website, one page leads to another page. Data is available on different pages through Sessions.
You can find the data you want on all pages. For example, when you do online shopping and you like an item, you add it to the cart and start seeing any other item. You get add to the cart which you like.
Finally, when you make a payment, all the items you show in one place are done by such sessions.
Cookies are used to start your session again. Cookies contain your session ID. When you visit a site, that website stores the session ID as a cookie on your computer.
When you visit the same website, the session is started with the same ID from the cookies. If the session ID is not saved, a new session is started and its ID is stored as a cookie.
<?php // Start the session session_start(); ?> <!DOCTYPE html> <html> <body> <?php // Set session variables $_SESSION["favcolor"] = "green"; $_SESSION["favanimal"] = "cat"; echo "Session variables are set."; ?> </body> </html>
Start a PHP Session
Working with sessions in PHP is very easy. PHP provides some predefined variables and methods for this. These are describe below.
The session_start () function session is use to start. This function first checks that no session already started. If there is no session, this function starts a new session. You should call this method first in your PHP script.
Get PHP Session Variable Values
Once the session created, you can create variables in it. All variables are automatically store in the $ _SESSION  array. In this array, you can store any value with its name. Its simple syntax is give below.
$_SESSION[‘variable’] = “value”;
Modify a PHP Session Variable
To change a session variable, just use it:
<?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // to change a session variable, just overwrite it $_SESSION["favcolor"] = "yellow"; print_r($_SESSION); ?>
Destroy a PHP Session
Most sessions destroyed when you close the browser window. Then the session destroyed as well. You can also destroy sessions manually. For this, PHP provides session_destroy () and unset () methods.
You can destroy the session by using the session_destroy () method. This also destroys the session variables with the function session. This function should call at the end of the PHP script. An example of this is give below.
If you want to destroy any single session variable, then PHP also provides a function for it. You can destroy any session variable by using the unset () function. For this, you call the unset () function and pass the session variable into it. An example of this is give below.
<?php session_start(); ?> <!DOCTYPE html> <html> <body> <?php // remove all session variables session_unset(); // destroy the session session_destroy(); echo "All session variables are now removed, and the session is destroyed." ?>