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)
Contents
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!
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.
I am Jaydeep Gondaliya , a software engineer, the founder and the person running Pakainfo. I’m a full-stack developer, entrepreneur and owner of Pakainfo.com. I live in India and I love to write tutorials and tips that can help to other artisan, a Passionate Blogger, who love to share the informative content on PHP, JavaScript, jQuery, Laravel, CodeIgniter, VueJS, AngularJS and Bootstrap from the early stage.