How to create an visitor counter with HTML & PHP?

Today, We want to share with you html visitor counter source code.In this post we will show you Page Hit Counter code/script, hear for website visitor counter code javascript we will give you demo and example for implement.In this post, we will learn about PHP Get country city location details with an example.

PHP Hit Counter (Page Hit Counter code/script)

this is a tinycode but not that simple hit counter. This is the best way to do it with a simple file as database and
You can also include it anywhere you whant, all compatible!

Read Also:  Laravel 5.4 Paypal integration - Paypal Payment Gateway

index.php

<?

session_name('mysite_hit_counter');
session_start();
//
$fn = 'total_visiotrs.txt';
$total_visiotrs = 0;
// read current total_visiotrs
if (($total_visiotrs = file_get_contents($fn)) === false)
{
	$total_visiotrs = 0;
}
// write one more hit
if (!isset($_SESSION['page_visited_already']))
{
	if (($fp = @fopen($fn, 'w')) !== false)
	{
		if (flock($fp, LOCK_EX))
		{
			$total_visiotrs++;
			fwrite($fp, $total_visiotrs, strlen($total_visiotrs));
			flock($fp, LOCK_UN);
			$_SESSION['page_visited_already'] = 1;
		}
		fclose($fp);
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Hit Counter Example Code - www.pakainfo.com</title>
</head>
<body>
<p>Page content...</p>
<div class="counter">
	<p>This page has <span><?=$total_visiotrs;?></span> visiotrs</p>
</div>
</body>
</html>

Usage
also you can used it on your webpage for downloads count. as well as use PHP with the most simple solution.
-> count unique visitors to your site

How to Make a Simple Visitor Counter Using PHP

index.php

<?php
session_start();
$online_live_total = "counter.txt";


if (!file_exists($online_live_total)) {
  $f = fopen($online_live_total, "w");
  fwrite($f,"0");
  fclose($f);
}


$f = fopen($online_live_total,"r");
$visitorsData = fread($f, filesize($online_live_total));
fclose($f);


if(!isset($_SESSION['hasVisited'])){
  $_SESSION['hasVisited']="yes";
  $visitorsData++;
  $f = fopen($online_live_total, "w");
  fwrite($f, $visitorsData);
  fclose($f);
}

echo "You are visitor number $visitorsData to this site";

Display the Number as an Image

$visitorsData = str_pad($visitorsData, 5, "0", STR_PAD_LEFT);
$akshars = preg_split('//', $visitorsData);

complete PHP code: index.php

<?php
  session_start();
  $online_live_total = "counter.txt";


  if (!file_exists($online_live_total)) {
    $f = fopen($online_live_total, "w");
    fwrite($f,"0");
    fclose($f);
  }

  $f = fopen($online_live_total,"r");
  $visitorsData = fread($f, filesize($online_live_total));
  fclose($f);


  if(!isset($_SESSION['hasVisited'])){
    $_SESSION['hasVisited']="yes";
    $visitorsData++;
    $f = fopen($online_live_total, "w");
    fwrite($f, $visitorsData);
    fclose($f);
  }

  $visitorsData = str_pad($visitorsData, 5, "0", STR_PAD_LEFT);
  $akshars = preg_split('//', $visitorsData);
  $im = imagecreatefrompng("canvas.png");

  $code1 = imagecreatefrompng("$akshars[1].png");
  $code2 = imagecreatefrompng("$akshars[2].png");
  $code3 = imagecreatefrompng("$akshars[3].png");
  $code4 = imagecreatefrompng("$akshars[4].png");
  $code5 = imagecreatefrompng("$akshars[5].png");

  imagecopymerge($im, $code1, 0, 0, 0, 0, 56, 75, 100);
  imagecopymerge($im, $code2, 60, 0, 0, 0, 56, 75, 100);
  imagecopymerge($im, $code3, 120, 0, 0, 0, 56, 75, 100);
  imagecopymerge($im, $code4, 180, 0, 0, 0, 56, 75, 100);
  imagecopymerge($im, $code5, 240, 0, 0, 0, 56, 75, 100);

  header('Content-Type: image/png');
  echo imagepng($im);
  imagedestroy($im);
?>

I hope you get an idea about how to add a visitor counter to my website html?.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  $event Object as Parameter using Angular Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Simple Like Dislike Event Handling in AngularJS
  2. Read Also:  how to add a visitor counter code javascript HTML?
  3. Read Also:  html visitor counter source code using PHP
  4. Read Also:  php read csv file into array Examples
  5. Read Also:  Update Query In Codeigniter Example Tutorial
  6. Read Also:  Vue watch two dimensional Array and Object
  7. Read Also:  Laravel Eloquent Useful Tips and Tricks
  8. Read Also:  Difference Between CSV and XLS file Formats
  9. Read Also:  How to display pdf file in HTML - PDF Viewer HTML5 Example
  10. Read Also:  CSS Modal Popup Box Window Plugins
CLOSEX