PHP | Basic Sessions Concept

What is a PHP Session?

A session is a global variable stored on the server.A session is a way to store information (in variables) to be used across multiple pages.

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 solve this problem by storing user information to be used across multiple pages. By default, session variables last until the user closes the browser. Session variables hold information about one single user, and are available to all pages in one application.

How to Declare Session ?

A session is started with the session_start() function.Session variables are set with the PHP global variable: $_SESSION.

How to set value in Session ?

<?php

session_start();
$_SESSION["x"] = 5 ;
$_SESSION["y"] = 10;

?>

So now in multiple pages value of variable x and y is fix. so we can use this values in any pages.

How to Get PHP Session Variable Values ?

Its Simple, to fetch session variable,

as above example,

<?php

session_start();
echo "value of x is" . $_SESSION["x"];
echo "value of y is" . $_SESSION["y"];

?>

Output is : 5 10

You may also like...

Leave a Reply

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