PHP Tutorial 3: Making a Click Counter
This tutorial is a click counter for OUT going links this can be used if you are advertising a site and want to know how many click they have had from your site.

Run this in phpmyadmin's query window
Code:
DROP TABLE IF EXISTS clicks;
CREATE TABLE clicks (
id int(10) NOT NULL auto_increment,
url text,
clicks int(11) default NULL,
date text,
PRIMARY KEY (id)
);
Save this page as click.php
Code:
<?
$url = $_SERVER['REQUEST_URI'];
$a123 = explode("?url=", $url);
$url = $a123[1];
$date = date("d/m/y"); // Grabs date

/* MYSQL CONNECT */
$db_host = "localhost";
$db_username = "username"; // Will most likely need your username as a prefix
$db_password = "password";
$db_name = "database"; // Will most likely need your username as a prefix
mysql_connect($db_host,$db_username,$db_password) or die(mysql_error()); // This just connects to your server, will
give an error message if it cant
mysql_select_db($db_name) or die(mysql_error()); // This selects the database to work on, this will give an error
if it cant aswell

$query = "SELECT * FROM clicks WHERE url='$url'"; // Query to find the url if it already
exists
$result = mysql_query($query);
while($r=mysql_fetch_array($result))
{
$clicks = "$r[clicks]";
$clicks++; // Adds one click to the total clicks
$end = "okay"; // This just tells the script that the url already exists and doesnt need to add a new
entry
$query = "UPDATE `clicks` SET `date` = '$date', `clicks` = '$clicks' WHERE `id` =
'$r[id]' LIMIT 1"; // Query to add a click to the url
mysql_query($query); // Add one more click to the url
}
if ($end != okay) { // If the url isnt already in the databse run the following lines
$query = "INSERT INTO clicks (url, clicks, date)
VALUES ('$url','1','$date')"; // Adds the url to the databse to start recoding
clicks
mysql_query($query);
}
header("Location: $url"); // Go To the link that you clicked on

?>
Now to use it just run click.php?url=http://link.com/