Tracking User Count using PHP

Tracking User Count using PHP

Tracking User Count using PHP. In this Article we  will look into tracking User Visit counts using PHP Program.

Previously in my blog i have go through how to Start a Session and using Cookies. Now our task is to combine everything and create a User Count Tracking program which will increment the number of visit count when a new visits is detected

There are a lot of method creating program in PHP to track User activities. We can do it by tracking the User using Sessions, so that User will not be able to turn off the Tracking script as this is written on the server side. Mean while we can also  track the User through Cookies , which can be turn off by the user web browser.

The Sample Code Below will show tracking User Count  using PHP via Cookies

  1. First we need to set Cookies Values for number of  Visits and cookies Time length (86400 equivalent to 1 day).
  2. Set Cookies values for “lasttime visit to store the timing of last visit
  3. Check if there is a Visits and then update the Visits value
  4. Check if there is a last time visit then update the last time visits value
  5. if no visit detected echo  ” First Time Visit “
  6. If visit detected echo the number of visits and the time of visits

Check out the Code Below


<?php

$visitnumber=0;

if(isset($_COOKIE['$visitnumber'])){
	
	$visitnumber= $_COOKIE['$visitnumber'];
	$visitnumber++;
}

if(isset($_COOKIE['$lastvisittime'])){
	
	$lastvisittime=$_COOKIE['$lastvisittime'];
}

setcookie('$visitnumber',$visitnumber+1,time()+86400);
setcookie('$lastvisittime',date("d-m-y H:i:s"),time()+86400);

if(!$visitnumber){
	
	echo 'Welcome To your First Visit';
	
}

else{
	
	echo'you have been visit number of'.$visitnumber.'time before at'.$lastvisittime.'<br>';
}

?>



This Code is being Implemented at the PHP Login Form Here
It will count the Number of times the User Visit the Page

Well that wrap up the Article on how to use PHP to write a tracking program to track User using Cookies. As a Responsible web master , we should always respect our User Will and let them choose whether they want to be tracked or else.User Personal information protection should always be set as the utmost priority, this is to ensure no sensitive information form the USER will be track or leak out to malicious parties.

Leave a Comment

four × two =