Understanding the concept of Session in PHP easily
Here, You can get the complete knowledge of the PHP Session without any doubt.
If you want to build Login-Registration system in PHP. I recommend you to go to my this article to get the entire idea of Login-Registration system with code snippets in PHP.
Now, Let’s talk about PHP Session.
What is a PHP Session?
When you work with any kind of software/application, you open it, do some changes, then you shut it. This is often very similar to a Session. Your PC knows who you’re . It knows once you start the appliance and once you end. But on the online there’s one problem: the web server doesn’t know who you’re or what you are doing.
This problem is solved by Session variables by storing user information to be used across multiple pages (e.g. username, favorite color, etc).
So, we can say Session variables hold information about one single user, and are available to all or any pages in one application.
Now, we discuss about how to start a PHP Session.
Start a PHP Session
A session is started with the session_start()
function.
Session variables are set with the PHP global variable: $_SESSION.
Let’s create a page called “session_1.php”. In this page, we start a new PHP session and set some session variables:
session_1.php
<?php// Start the session
session_start();?><!DOCTYPE html>
<html>
<body>
<?php// Set session variables$_SESSION["username"] = "admin";$_SESSION["fcolor"] = "blue";echo "Session variables are set.";?>
</body>
</html>
Now, Let’s discuss about how to get a values from PHP Session.
Get PHP Session Variable Values
Let’s create another page called “session_2.php”. From this page, we will access the session information we set on the first page (“session_1.php”).
session_2.php
<?php// Start the session
session_start();?><!DOCTYPE html>
<html>
<body>
<?php// Echo session variables that were set on previous page
echo "Username is " . $_SESSION["username"] . ".<br>";echo "Favorite color is " . $_SESSION["fcolor"] . ".";?>
</body>
</html>
Here we discuss about how to modify a values of PHP Session.
Modify a PHP Session Variable
To modify/change a session variable, just overwrite it:
session_3.php
<?php// Start the session
session_start();?><!DOCTYPE html>
<html>
<body>
<?php// to change a session variable, just overwrite it
$_SESSION["fcolor"] = "yellow";print_r($_SESSION);?>
</body>
</html>
Now, Let’s discuss about how to destroy a PHP Session.
Destroy a PHP Session
To remove all global session variables and destroy the session, use session_unset()
and session_destroy()
:
session_unset()
remove all session variablessession_destroy()
destroy the session
session_4.php
<?php// Start the session
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."?>
</body>
</html>
If you want to make your Web UI more attractive you should definitely checkout below articles.
I hope you’ve found this article informative and helpful. I’d love to hear your feedback!
Thanks for reading! :)