SESSION in PHP

A session is a way to store information (in variables) to be used across multiple pages.

Unlike a cookie, the information is not stored on the users computer. Session variables hold information about one single user, and are available to all pages in one application.

When you work with an application, you open it, do some changes, and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are or what you do, because the HTTP address doesn’t maintain state. Session variables hold information about one single user, and are available to all pages in one application.

How To Start a SESSION ?

A session is started with the session_start() function.

Session variables are set with the PHP global variable: $_SESSION.

Now, let’s have an example of Session. In this, we start a new PHP session and set some session variables:

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body><?php
// Set session variables
$_SESSION[“email”] = “abc@gmail.com”;
$_SESSION[“phone”] = “9999999999”;
echo “Session variables are set.”;
?></body>
</html>

-The session_start() function must be the very first thing in your document. Before any HTML tags.

Leave a Reply

Your email address will not be published. Required fields are marked *