PHP is open source scripting language. It\'s widely used to develop web applications.  Home Web Programming PHP Hit Counter
Your Ad Here

Hit Counter


Hit CounterHit counters are used to track the amount of visitors there have been to a cetain page. I have looked through the internet and I have come up with 2 different ways to produce a hit counter.

  1. Flat Files (Storing data in a *.txt or *.dat file)
  2. Database (using MySQL or something similar to store the data in)

Each way is the same, there is no difference between them. I have written a description on each of these 2 methods together with the scripts to produce your own hit counters. Each script will be given with a line by line explanation of all the details of the code.

Flat Files

Storing data in a *.txt or *.dat file. Now the script will create the file for you if you haven't already created it so you don't need to worry about that.

<?php
$pagehits = ("hits.txt");
// Name of the file the hits will be stored in (hits.txt)

$hits = file($pagehits);
// Defining a variable for our file the hits will be stored in

$hits[0] ++;
// Takes our number and adds one to it

$fp = fopen($pagehits, "w");
// Open our file with the write permission enabled

fputs($fp , $hits[0]);
// Writes to our currently open file our new number

fclose($fp);
// Now closes the file

echo 'Total Hits: $hits[0]';
// Display the output (amount of hits)

?>

This is the way to create a hit counter using a flat file. The second option uses MySQL to keep track of all the hits to your site.

To start you will need to create your database, I will assume that you already know how to do this, lets call the database hits. The second thing you need to do is add a query to our database, here is the query:

CREATE TABLE 'counterlog' (
'hits' int
);

We have just created a table called counterlog and a table field called hits. In this field we will be storing the amount of hits.

<?php
$conn = mysql_connect("host", "username", "password");

mysql_select_db("database");

$counter = mysql_query("select hits from counterlog");

$row = mysql_fetch_array($counter);

$hits = $row['0'];

if (!$hits) { $hits = 0; }

$hitss = $hits + 1;

echo 'People Who Have Read This Tutorial : '.$hitss.'';

$update = mysql_query("update counterlog set hits='$hitss'");

mysql_close();

?>

Lets break down this code to plain English shall we.

<?php // Opening PHP tag
Connect to my server using these details and give all this a variable name 'conn'
Connect to this database

Select the fieldname hits from the table counterlog and give this a variable name of 'counter'

Returns an array that corresponds to the fetched row, or FALSE if there are no more rows

Takes the field hits from counterlog and give them all a variable name 'hits'

Here we just want to assign 0 to $hits if there is nothing in the database

Take our variable 'hits' and add 1 to it each time

Print our output onto the page

update the new 'hits' variable, the script takes the value from the database, adds one to it and now this writes the new value back to the database

close our database connection
?> // Closing PHP tag

You will now be able to have your custom hit counter on your page with the speed of mysql, enjoy!



Author's URL: Joseph Skidmore
Thank you for voting.
Rate this Materials:
Bad 
1 2 3 4 5 Excellent
print this page subscribe to newsletter subscribe to rss

Web programming � everything from the basics of visual design and architecture to the specifics of applications, graphics, and scripting. More Web Programming: Most Popular Materials | Fresh Materials | More PHP Tutorials at LearnPHP.org

Add comments to "Hit Counter"

Only registered users can write comment

No comments yet...