Session In PHP

How Session Works? Session work by creating a unique identification(UID) number for each visitor and storing variable based on this ID. This helps to prevent two user data from getting confused  with another when visiting the same webpage. The UID is either stored in a cookie or is propagated in the URL. Starting a PHP Session Before you can store user information in your php session, you must first start up the session. The session_start() function must appear before the <html> tag.

<?php
session_start();?>
<html>
<body>
....
....
....
....
</body>
</html>
Storing a Session Variable The correct way to store  and retrieve session variables is to  use the PHP $_SESSION  variable.
<?php
session_start();
//store session data
$_SESSION['views']=1;
?>
<html>
<body>
....
....
....
....
</body>
</html>
Retrieving a Session
<?php
session_start();?>
<html>
<body>
echo "Pageviews=".$_session['views'];
</body>
</html>
Output : Pageviews=1 Destroying A Session The unset() function is used to free the  specified session variable.
<?php
unset($_session['views']);
?
You can also completely destroy the session by calling the session_destroy() function :
<?php
session_destroy();
?>
session_distroy() will reset your session and you will lose all your stored session data.]]>

Anuj Kumar

This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
.

Recommended Tutorials for you

Add comment

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.