Counting Clicks with PHP

Posted January 16th, 2006 by Mike Cherim

A rather simple PHP solution to help you count your clicks.

If you know me, you know I like stats. I’ve got no problem with putting a hit counter (though not a rented or external one) on one of my sites. In fact my Home Page has one at the footer. It’s just for my portfolio and doesn’t record my unique blog hits — approaching 10,000 — that’s kept on a separate counter which isn’t displayed. You might also notice the counters I have on my blog’s side bar. I like numbers. It’s good to know if anyone’s online, how many visitors I’ve had, or how many times something has been downloaded. If I didn’t count these things, I would never know that my Beast-Blog theme, for instance, has been downloaded almost 2000 times as of this writing. And trust me when I say I would have never guessed the number would be so high. Unlike my site hit counter which maintains an IP address list and records unique hits only, the click counter doesn’t record unique persons downloading, but each number represents a link-click and subsequent *.zip file download. Yep, numbers can be interesting, and telling.

One example of good-to-know numbers would be for self-maintained site advertising. If you provide independent advertising opportunities on your website, your sponsors (and you) may want to know just how effective these ads are. They don’t have to be pay-per-click for this to be useful. Knowing stats is useful all the same. It will help determine if an advertisement or location is producing visitors. Or it’ll tell you how may times someone downloaded your price list or resume. The latter is good to know if your resume has been download twenty thousand times and you’ve only had two jobs, this is useful information.

Today I will share with you a rather simple PHP solution to help you count your clicks.

To make a click counter you will create two files: a PHP script file and a text file for the output. Please note first that if you plan to have multiple, specific-function click-counters on your site, you will want to name these files in a way that will help you avoid confusion. Assuming here that we want to count clicks to a file called “targetgif.gif” we should name the files as targetgif.php and targetgif.txt. First let’s make the script:

The Script

Here’s the scripting:

<?php
  $file = 'targetgif.txt';     // (see 1 below)
    $data = @file($file);
    $data = $data[0];
  if($handle = @fopen($file, 'w')){
    $data = intval($data); $data++;
  fwrite($handle, $data);
  fclose($handle);
}  // (see 2 below)
  header('Location: http://green-beast.com/blog/examples/images/targetgif.gif');
?>
  1. This is the path/name of the output text file. Relative only to the targetgif.php file.
  2. The actual target location (I used a full URL to ensure it works from anywhere, like my RSS Feed, for example).

The Output

This is a blank text file. Make sure the path is right and set its CHMOD permissions to 666. That’s it.

The Link

To make this work as you want, you need to send your page-visible link not to the target, but rather to the script file. In this case the link will lead to targetgif.php. from there it’ll redirect and count. This happens so quickly that it is not noticeable.

The Count

To view the counter’s output you can bring up the text file directly. In this case that would be bring up the path and file for targetgif.txt. You will bring up a page containing the click count number and that’s it. You may wish to display this number, or place it on a web page somewhere. A simple snippet of code will allows us to do just that. Like so:

<?php
  readfile("http://green-beast.com/blog/examples/targetgif.txt");
?>

Or a simple include will work, like this

<?php
  include("http://green-beast.com/blog/examples/targetgif.txt");
?>

The Target Test

So let’s go ahead an try this out. I’m using the actual examples above. First I will offer you this targetgif.php file link (which will lead you to the image file): View My Target Image. If you clicked on the link and it worked you were immediately sent to the image. You hit your “Back” button and returned here. By doing this you added one number to this actual count (using the “readfile” string above): This image has been viewed 6938 times. Go ahead, try it again if you missed that (remember to refresh). When you return to this page the number will have increased by one.

Important Update: If you find the text file is not being written to and the path is right and it’s writable, then try putting the number 1 in the text file manually. It should record without issue after that. Don’t know why this is, but the pump sometimes needs to be primed.

That’s it. Have fun. Go count some clicks.


8 Responses to: “Counting Clicks with PHP”

  1. Fabian responds:
    Posted: January 18th, 2006 at 9:13 am

    Very Nice once again Mike, for some reason you were not on my blog roll I appoligize for that, I must of lost you when I was transferring hosts, well your back on there now. :)

  2. Martin Neczypor responds:
    Posted: January 26th, 2006 at 2:48 pm

    Great tip/write up Mike. I was looking for something like this, and now that I know how to do it I’ll definately make use of it in the future.

  3. Tom responds:
    Posted: January 27th, 2006 at 8:24 pm

    Hi

    I found your script and wonder if this can be used to count the number of times a link is clicked?
    Sorry, but I am a newbie to stuff like this, and wondered if this can be used as a link counter;

    1. Am I allowed to use your script?
    2. As mentioned, I am a newbie, how do I make this script work?

    Do I upload the script to my server or put it in the document I want to use it on?

    Any reply/help would be much appreciated.

    regards,

    Tom

  4. Mike responds:
    Posted: March 2nd, 2006 at 6:04 pm

    I was wondering… I have mysql db that has a link table with 4 fields (id, url, descrip, category, hits) I can pull all the info from the table, but I would like to be able to increment the hits field for each link clicked (without have to ‘install’ someone elses bulky script) It seems like such a simple task, but I haven’t been able to figure it out or find anything useful yet.

    Thanks in advance
    -Mike

  5. JKL responds:
    Posted: April 1st, 2006 at 3:13 pm

    Q: chmod 666 ?
    A: no
    Solution: chmod 664; chgrp www-data

    Isn’t that much better idea?

Sorry. Comments are closed.




Note: This is the end of the usable page. The image(s) below are preloaded for performance only.