[REQ] Click tracking script to track clicks

matt1972

Power Member
Joined
Nov 19, 2009
Messages
503
Reaction score
206
I'm in need of a good click tracking script to track my click for Clickbank. Anyone have anything they could share? Thanks.
 
Here is the simplest solution that will count a click and then redirect to a URL.
Create a "clicks.txt" in the same place as the PHP script.


PHP:
<?php

$fname = "clicks.txt";
$fsize = filesize($fname);

$fp = fopen($fname, 'r');
$clicks = fread($fp,$fsize);
fclose($fp);

$clicks += 1;

$fp = fopen($fname, 'w+');
fwrite($fp, $clicks);
fclose($fp);

header('Location: http://www.YOUR URL.com');


?>
 
This is a shorter version but requires PHP5 or greater. I would suggest doing a database solution instead.

PHP:
<?php
	$file = 'clicks.txt';
	$clicks = file_get_contents($file);
	file_put_contents($file, ($clicks+1));
	
	// The rest of your script would go here
	// OR to redirect them to the URL:
	header("Location: http://www.google.com");
?>


Or you can use the SQL version I whipped up. Create this mySQL table:

Code:
CREATE TABLE clicks` (
`click_id` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`click_url` VARCHAR( 1000 ) NOT NULL ,
`click_time` INT( 11 ) UNSIGNED NOT NULL ,
`click_ip` VARCHAR( 16 ) NOT NULL
) ENGINE = MYISAM ;

And use this code whereever you need it:

PHP:
<?php
        $c = mysql_connect('localhost', 'your sql user', 'your mysql pass') or die("Error connecting to mySQL: " . mysql_error());
        mysql_select_db('your mysql db') or die("Error selecting database: " . mysql_error());

        function smart_quote($q) {
                return "'" . mysql_real_escape_string($q) . "'";
        }

        mysql_query("INSERT INTO `clicks` SET `click_url` = " . smart_quote($_SERVER['REQUEST_URI']) . ",
                `click_time` = " . smart_quote(time()) . ",
                `click_ip` = " . smart_quote($_SERVER['REMOTE_ADDR'])) or die("Couldn't execute query: " . mysql_error());
?>

Tested and working. If you want to count the clicks, you'll need to either look in your database manually, or make a short script to SELECT.

Thanks, Ben
http://www.ntertech.com/
 
nezZario, thanks for the script. It is working good.

1. It also tracks .csss or .js files, need to avoid that... How we can add dont track some specified folders or files?
2. Importantly, it is not tracking outgoing urls. It only tracks site urls.. Any idea we can add outgoing url tracking?
3. Any idea how to get a report of most clicked urls?

Thanks
 
Wouldn't prosper202 do the same and more?
Posted via Mobile Device
 
prosper202 is very good for this and free
 
Back
Top