This website looks best on firefox.
|
|
Creating a Hit counter
Introduction This article will give you a case study kind of description for creating a hit counter, and further hosting it as a hitcounter service.
This script was initially written as the beta version of the hitcounter service hosted by this site floresense.com
Though the code attached is developed in PHP for MySQL, this article clearly puts out a requirement and implementation spec which can be used by any developer.
The Case: We need to set up a hitcounter. Since we would like to use the hitcounter at many websites, we would prefer developing it as a service hosted on a parent website. All webpages which need to generate visitor stats will use an URL to the parent website.
The parent server which has the hitcounter updates its database with the hitcount for the calling website, and also sends back a graphical image for the hitcount.
Implementation Logic: We will get user email, and exact url where counter will be placed.
We will also get a value depending on counterType that the user wants. 0 = Text counter, 1 = graphic counter. Only two modes are done in this project.
hitcounter.php is the counter generator script.
When a webpage makes a call to hitcounter.php, we receive the referring url, and the ip address of the client machine. We check this url in our database 'registrations', if not found we ignore the rest of the process.
If found, we check for the combination, url, client ip address, current date, in the 'hits' database. This is to check whether client has visited the page from same machine on the day.
If yes, we update the 'hitcount' field in this record in 'hits' table. If No, we create a new record.
Number of hits is sum(hitcount) for a particular url, between the currentdate and the 'counterstartdate' in 'registrations' table. Counter resetting function is not provided in the download. Suppose a resetting function is to be added, it would reset the 'counterstartdate' in 'registrations' table to currentdate.
Reporting scripts are not there in the download attached with this article. The content in the hits table is a simple yet good report on hits, from which one can find out geographic distribution of visitors based on IP address, and average hits per day, and a lot more.
Navigation Plan: 1 User enters site, enters data in Free Counter registration form in default.hm 2 Form is submitted to FCsignup.php 3 FCsignup.php validates data (p.s. two validation functions are written) 4 If no error (PHP variable $Errstring is empty in FCsignup.php), redirects user to FCdone.php 5 If error, display HTML with error message in FCsignup.php
6 User receives a mail regarding setup 7 He places a one-line code in his page where the counter should appear.
8 This one liner is a call to hitcounter.php which will validate user depending on referral URL, and show the counter.
At this point, if you are a PHP and mySQL person, download code from above link and read below for instructions on this code. Or, directly read the "scope for modifications" section below:
Dependencies: The code assumes that the server on which this application is hosted has the following:
1 PHP version. 4.3.3 or higher 2 GD library and dependencies installed for working with .png images. [ Lastest versions of PHP have an inbuilt GD library and its dependencies. But on Windows machines running it might still require some tweaking. This note is for people who are still with php 4.0 or below] 3 An SMTP server for mails. [A portion of the code sends an email to the newly registered user. This code is in FCsignup.php]
Advertisement
|