PHP Click Counter

This tutorial will tell you how to code a counter that counts how many times a link is clicked.

First, make a folder for your click files. If you plan on having many counters it'll be very messy if you don't make a folder. So make one and name it something like 'click'.

Open a new notepad file and name it 'click.txt', but leave it completely blank. Save it to the click folder. (When you upload it to the FTP, upload this file in ASCII mode. Just this file, nothing else.)

Now, open a new flie and paste this code in there:

<?php $file = 'click.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://www.insertdestinationhere.com'); ?>


Change the bolded url at the bottom the destination that you want the link being counted to go to. Now, save this file as 'click.php' in the click folder.

Now to show the counter. Open the file where the click counts will be shown. Where your link that you want counted is, replace the url with this:

<a href="http://www.yoursite.com/click/click.php">[Link Name]</a> <?PHP include("click/click.txt"); ?>

All you have to do here is change the yoursite.com to the url of your site, but keep the click parts. You can change the "[Link name]" to any name you like, it's what will show up on the page.

I'll explain how this works (If you don't understand after reading this it's okay, you don't really need to know), the first link links to the php file, which sends info to the text file, and leads you to where the link is meant to lead. That little php code at the end shows the number of clicks stored in the text file. You see, the text file is not blank after the first click. The php file increases the number by one in the text file every time someone clicks.

Now, upload all the files to the FTP (if you haven't already), remembering to upload the text file (ONLY THE TEXT FILE) in ASCII mode.

The counter should now be functional! Click the link just to test it yourself. If it doesn't show the count, just refresh the page.

Have any problems? Email me: silveztria@yahoo.com


 Affiliates

 AobaruNet  Eevee Club House  Eevee~Valley  Eevee's Mansion  Luster Purge  Pikachu's Plaza  PikaPower HQ  Poké Shore  The Forest Manor  Treeshroud Forest  Twilight Harbour  United Universe
More?

 Monthly Poll

HeartGold or SoulSilver?
HeartGold!
SoulSilver!
Don't care.
Wtf are those!?
THIS POLL IS EVERYWHERE!! D<
See Results