The Wayback Machine - https://web.archive.org/web/20240722124053/https://www.geeksforgeeks.org/php-program-count-page-views/
Open In App

PHP Program to count Page Views

Last Updated : 15 Jan, 2018
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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:




<?php 
  
session_start();
   
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+1;
else
    $_SESSION['views']=1;
      
echo"views = ".$_SESSION['views'];
  
?>


Output:

Explanation
Below is the explanation of above code:

  1. 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.
  2. $_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.
  3. 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:




<?php
  
session_start();
$views = $_SESSION['views']; 
   
unset($_SESSION['views']); 
session_destroy();
  
 ?>


Output:



Similar Reads

Tracking Blog Page Views on Blogger
In this article, we will see how we can manage the tracking of your own blog pageviews on blogger by doing a simple configuration on select browsers. With this setup, you can control the pageviews of your own blog with your select browser or browsers. If you are using multiples pc then you can configure the same on other pcs as well with that blog
2 min read
Difference between views and templateUrl in AngularJS
In this article, we will see what is Views and TemplateUrl in AngularJS, along with understanding their basic usage through the code snippets &amp; finally see some differences between them. A Views is anything that a user sees on a screen and is used to set up multiple views or to target views manually. Angular uses views to connect with the platf
4 min read
ReactJS Semantic UI Advertisement Views
Semantic UI is a modern framework used in developing seamless designs for the website, It gives the user a lightweight experience with its components. It uses the predefined CSS, JQuery language to incorporate in different frameworks. In this article we will know how to use advertisement views in ReactJS Semantic UI. Advertisement is used to displa
2 min read
ReactJS Semantic UI Item Views
Semantic UI is a modern framework used in developing seamless designs for the website. It gives the user a lightweight experience with its components. It uses the predefined CSS, JQuery language to incorporate in different frameworks. In this article, we will know how to use Item Views in ReactJS Semantic UI. Item Views is used to present a set of
2 min read
ReactJS Semantic UI Feed Views
Semantic UI is a modern framework used in developing seamless designs for the website, It gives the user a lightweight experience with its components. It uses the predefined CSS, JQuery language to incorporate in different frameworks. In this article, we will know how to use Feed Views in ReactJS Semantic UI. Feed Views is used to present user acti
2 min read
ReactJS Semantic UI Card Views
Semantic UI is a modern framework used in developing seamless designs for the website, It gives the user a lightweight experience with its components. It uses the predefined CSS, JQuery language to incorporate in different frameworks. In this article we will see how to use card views in ReactJS Semantic UI. Properties: Cards: This property is used
2 min read
ReactJS Semantic UI Statistics Views
Semantic UI is a modern framework used in developing seamless designs for the website, It gives the user a lightweight experience with its components. It uses the predefined CSS, JQuery language to incorporate in different frameworks. In this article we will know how to use Statistics Views in ReactJS Semantic UI. Statistics Views are used to show
2 min read
jQWidgets jqxScheduler views Property
jQWidgets is a JavaScript framework for making web-based applications for PC and mobile devices. It is a very powerful, optimized, platform-independent, and widely supported framework. The jqxScheduler widget is used to show a set of appointments in a day, week, month, timeline day, timeline week as well as timeline month views. The views property
3 min read
Semantic-UI Views Complete Reference
Semantic UI is an open-source framework that uses CSS and jQuery to build great user interfaces. It is the same as a bootstrap for use and has great different elements to use to make your website look more amazing. Semantic-UI AdvertisementSemantic-UI Advertisement TypesSemantic-UI Advertisement Common Unit TypeSemantic-UI Advertisement Mobile Type
2 min read
Backbone.js js Models and Views
Models: Backbone.js Models are a very important part of building applications. The model manages an internal table of data attributes and triggers change events when any of the data in the table is modified. It performs many actions on the data such as like access control, validation, computed property, etc. Model handles syncing data with a persis
3 min read
Article Tags :
three90RightbarBannerImg