Session In PHP

A session allow to store user information on the server for later use.(i.e. username,shopping cart items,etc).
However,this session information is temporary and is usually deleted very quickly after the user has left the website that uses session.
Session variable hold information about one single user, and are available to all pages in one application.
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.

Storing a Session Variable
The correct way to store  and retrieve session variables is to  use the PHP $_SESSION  variable.

Retrieving a Session

Output : Pageviews=1
Destroying A Session
The unset() function is used to free the  specified session variable.

You can also completely destroy the session by calling the session_destroy() function :

session_distroy() will reset your session and you will lose all your stored session data.

Anuj Kumar

I'm Anuj kumar a 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 is provide php , jquery , mysql , phpoops and other web development tutorials. I am trying best effort to make PHPGurukul useful for every single moment spend on this website. If you think this website is useful to visit please share with your friend and buddies.

Recommended Tutorials for you

You may also like...

Leave a Reply

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