What is a session?
A PHP session is used to store data on a server rather than the computer of the user.Session identifiers or SID is unique numbers which are used to identify every user in a session based environment.The SID is used to link the user with his information on the server like posts, emails etc. You can learn about sessions in details in the article PHP | Sessions
How to use sessions for Storing Page Counts
A session mechanism can be used to store page views which increment on each refresh and show the count on a webpage. A session is user specific and for every user, a separate session is created along with a separate session variable which is associated with that session.
Using this mechanism, for every user the session variable is set to 1 initially for the first visit.On consecutive visits, the value of this session variable is incremented and displayed on the output webpage.
Below is the PHP program to store page count:
Below is the explanation of above code:
- session_start() :It is a first step which is used to start the session. It is a standard call. The session_start() should be used whenever the session variable is used.
- $_SESSION[‘views’] :This is the session variable which is used to store views count for a user’s session. ‘views’ is the session name. The session name should be always be enclosed within the single quote.
- isset() : It is a standard php function which returns true or false depending upon whether the passed parameter is set or not.
We can also reset the session variable. Below program shows how to reset a session in PHP:
- How to pass form variables from one page to other page in PHP ?
- How to display search result of another page on same page using ajax in JSP?
- How to show Page Loading div until the page has finished loading?
- How to redirect a page to another page in HTML ?
- Refresh a page using PHP
- How to add API function to a simple PHP Page ?
- How to create admin login page using PHP ?
- How to minify HTML code of PHP page ?
- How to connect the Database with PHP DOM page ?
- How to add a PHP page to WordPress?
- Python program to crawl a web page and get most frequent words
- How to hide an element when printing a web page using CSS?
- HTML | Responsive full page image using CSS
- Download web page using Java
- jQuery | Page Piling Plugin
- CSS page-break-before Property
- HTML Course | First Web Page | Printing Hello World
- CSS | page-break-after Property
- CSS | page-break-inside Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.