This is a simple little bit of code that can be used to count links going off your site. It was originally written to count click-throughs for a 'Top Sites' program, but could be useful for any site that needs to count how many outgoing links are clicked, and to what pages.
More PHP / MySQL Quick Tips
This code can be saved as add.php. It assumes that you have a database in place where the url's of sites are stored along with a unique site ID number. We use this ID number to reference the site in links and in the tracking. The auto-incriment field type tends to work best for this in MySQL.
<?phpThe link on your actual page would look like this:
// Connects to your Database
mysql_connect("your.hostaddress.com", "username", "password") or die(mysql_error());
mysql_select_db("Database_Name") or die(mysql_error());
//Adds to the click count for a particular link
mysql_query("UPDATE table_name SET out = out + 1 WHERE ID = $id")or die(mysql_error());
//Retrieves information
$data = mysql_query("SELECT * FROM table_name WHERE ID = $id") or die(mysql_error());
$info = mysql_fetch_array($data);
//redirects them to the link they clicked
header( "Location:" .$info['URL'] );
?>
<a href="http://www.yoursite.com/out.php?id=4">Some Website</a>The link above would add 1 to the count on the site with the ID of 4, and then direct the users to the website.

