Overview: In this article we will discuss what are Sessions and how to work with sessions in PHP.
Imagine that you have some data about a particluar user that you need on all of the pages, woudn't it be great if you could store that info in a variable and access it from any page, Sessions exactly do that.
Using sessions you could hold data about particluar user and access that info on all the pages as long as the user is active on the site.
* Each session holds different values for different user.
* Sessions automatically die when the users session is over or he closes his browser.
PHP's session-handling features, was first introduced in the 4.0 release, Sessions are native and thus require no additional packages.
To work with Sessions we will be using session_start() and session_register() functions, its important that you add sesions_start to every page where you would like to use sessions.
I will explain you the conecpt with an example.
// create a new session
// register a session-variable
session_register("color"); //we have created a session with name color
$color = "blue"; //assigning some value to our session
<a href = "session2.php">Go to another session-enabled page</a>.
Okay now we have created a session with name color and assigned it as blue, now in our second programme (session2.php) we access the value of our session color
echo "The value stored in your session: " $color; //retriving value from the session
To delete a session variable we use the function session_unregister() so to delete our session color we would use the code
| Terms of Service
(c) 2002 - 2020 PHPbuddy.com Unauthorized reproduction/replication of any part of this site is prohibited.