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!


Related Articles

  • Simple Hit Counter
    Use this simple script for a basic click counter to display the amount of hits to your site.
  • Counter in ASP
    A hit counter is an essential part of a site to know how many people are coming to site. Here is an easy way to make a counter. All you need is access to ASP and be able to give a write access to a directory...
  • A Nice Hit Counter
    I shows
    Total Hits Unique Hits Todays Hits Todays Unique Hits
  • Easy Flat File Hit Counter
    Create a very simple hit pageview counter without any database! Use flat file scripting to make an easy counter!
  • Hit Counter
    Hit 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.
  • PHP Counter
    A counter is a simple yet essential script when developing web pages.
    First of all, we need a file where to store the data ( a simple text file). This file must be CHMODed to 777 (under Linux)
    Lets name this file data.txt. Do not forget to include it into the same directory as the PH...
  • Outwards Link Counter
    This will allow you to create a script that will count the amount of times a certain link has been clicked and display the results on your page.
  • What are Usage Statistics?
    Usage Statistics are very important to have. They give you endless amounts of data that you can use to shape the future of your own Web site. With a few clicks of the mouse, you can find out if your latest idea is working or what you might need to do to get more visitors to your Web site...
  • Downloads CMS and Clicks Counter
    1. There are 3 documents we will be creating: downloads.php, downloaditem.php, additem.php. You will need access to a mySQL Database with phpmyadmin, as well as a host with php installed onto it.
    2. First lets set up the table in phpmyadmin. go to your database, and run the following SQL Comman...
  • Download Counter with Apache and PHP
    Info: Collect statistics on the popularity of your downloads with Apaches mod_rewrite and PHP. Some sites present you an URI like Example.com, which is less than perfect. Being a Windows user (well sometimes) I expect that when I add a file to my download manager Ill see the filename in the list.....

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.