How To Create Page View Hit Counter Code Using PHP Script

Share:

How To Create Page View Hit Counter Code Using PHP Script. A website has been online will never be complete if not accompanied by a hit counter that can be utilized to analyze the development of blog trafic. Here, we will discuss how to create a page view counter code with the help of php scripts so that you can know how many readers of every page in your website.
How To Create Page View Hit Counter Code Using PHP Script

How To Create Page View Hit Counter Code Using PHP Script

You are curious to know how to create the page views hit counter. Let’s follow the simple tutorial below :

Download Now How To Create Page View Hit Counter Code Using PHP Script

Create A .txt File
First, we must create a txt file as a place to count page views a web page. The contents of the file by the first number is 0, then save the file name: pageview-counter.txt

Create A PHP Script
Next is to utilize the PHP fopen function using the parameters r+ and w+.
Explanation :
r+ = Open for reading and writing, place the file pointer at the beginning of the file.
w+ = Open for reading and writing, place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
Here is the complete php script to create a page views hit counter :
pageview-counter.php

<?php
function pageview_counter(){
if (isset($visitor)){
if ($visitor=="visited")
include("pageview-counter.txt");
} else {
$file=fopen("pageview-counter.txt","r+");
$result=fread($file,filesize("pageview-counter.txt"));
fclose($file);
$result += 1;
$file=fopen("pageview-counter.txt","w+");
fputs($file,$result);
fclose($file);
include("pageview-counter.txt");
}
}
?>

Display Page view Hit Counter In Website Page
Upload pageview-counter.php into the website hosting where your website is hosted there. If you want to show the page views hit counter on the web page then paste, this php include statement upon html code pages:

<?php include("pageview-counter.php"); ?>

And to show the number of page views, put the following script in the body tag:

<?php echo pageview_counter(); ?>

For example, look at the html code example page below :
pageview-counter-result.php

<?php include("pageview-counter.php"); ?>
<html>
<head>
<title> How To Create Page View Hit Counter Code Using PHP Script </title>
<style>
body{
background: #ddd url('http://1.bp.blogspot.com/-jaZSIgdhj6M/UkPKEAgZBnI/AAAAAAAABAw/aIQvBkQaSMM/s1600/tutorial-world-edu-background-black.png') repeat top left;
}
</style>
</head>
<body>
<h1 style="text-align:center;"><a style="text-decoration:none;color:#fff;" href="http://tutorial.world.edu/web-development/how-to-create-page-view-hit-counter-code-using-php-script/">How To Create Page View Hit Counter Code Using PHP Script</a></h1>
<div style="color:#fff;margin:50px 350px;padding:10px 22px 10px 22px;font-size:20px;background: url('http://1.bp.blogspot.com/-yXeAVNxY3Yg/UqAH7GmLEpI/AAAAAAAABLk/9LbyDGsQJSM/s1600/blackboard-tutorial-world-edu.png') no-repeat top left;background-size:640px 300px;">
<p><strong><a style="text-decoration:none;color:#fff;" href="http://tutorial.world.edu">Tutorial.World.Edu</a></strong>. A website has been online will never be complete if not accompanied by a hit counter that can be utilized to analyze the development of blog trafic. Here, we will discuss how to create a page view counter code with the help of php scripts so that you can know how many readers of every page in your website.</p>
<p>Pageview this article : <strong><?php echo pageview_counter(); ?></strong></p>
<br/>
<br/>
<br/>
<br/>
</div>
</body>
</html>

The result will be like this image :
How To Create Page View Hit Counter Code Using PHP ScriptWhat do you think ? This php script is very simple and easy, certainly you can practice it on the website without any constraint. Good luck and good luck ..
Thanks for reading How To Create Page View Hit Counter Code Using PHP Script

[Tutorial.World.Edu]

2 thoughts on “How To Create Page View Hit Counter Code Using PHP Script

Leave a Reply

Your email address will not be published. Required fields are marked *