Sometimes you need to count clicks on a particular link or image etc to know
how much successful you're with that particular link or image, right? Or, if you
get local ads on your site or you advertise your business on another site - you
may want to know how many times those are being clicked, no? This code comes to
help you there. This Simple Click Count PHP Code would be
particularly helpful 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.
Knowing stats is useful. It will help determine if an advertisement or location
is producing visitors.
To make a click counter you will create two files: A PHP script file and a text file for the output.
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 “test.gif” we should name the files as test.php and
test.txt. First let’s make the script:
The Script: Here’s the scripting:
<?php
$file = 'test.txt'; // (see 1 below)
$data = @file($file);
$data = $data[0];
if($handle = @fopen($file, 'w')){
$data = intval($data); $data++;
fwrite($handle, $data);
fclose($handle);
}
header('Location: http://site-destination.com/'); // (see 2 below)
?>
1.This is the path/name of the output text file. Relative only to the test.php file. It means these two file should be in the same folder.
2.The actual target location.
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 test.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 test.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://yoursite.com/test.txt"); ?>
Or a simple include will work, like this
<?php include("http://yoursite.com/test.txt"); ?>
Hope it's easy enough for you to put it all together!
The Script: Here’s the scripting:
<?php
$file = 'test.txt'; // (see 1 below)
$data = @file($file);
$data = $data[0];
if($handle = @fopen($file, 'w')){
$data = intval($data); $data++;
fwrite($handle, $data);
fclose($handle);
}
header('Location: http://site-destination.com/'); // (see 2 below)
?>
1.This is the path/name of the output text file. Relative only to the test.php file. It means these two file should be in the same folder.
2.The actual target location.
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 test.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 test.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://yoursite.com/test.txt"); ?>
Or a simple include will work, like this
<?php include("http://yoursite.com/test.txt"); ?>
Hope it's easy enough for you to put it all together!
No comments:
Post a Comment