PHP Scripts

Track and record referrals to a URL

This script updates a MySQL database, recording traffic from your website to a client URL.

It works by simply incrementing any record in the database, or adding a new entry if none exists, and then referring the browser page on to the URL.

2 pages are involved, a source page, and the count page, in this example index.htm and go.php.

Clicking "View the Client Website" form button posts several hidden variables, (if needed) as well, which are stored in the MySQL table. The table in this example has hits, url, email and dir as 4 fields, where hits is the number of referrals, url the destination website, email simply to make posting monthly records easy, and dir the source directory also for posting monthly records.

Page VIEWS and then Page REFERRALS like this can give clients a fair idea of the exposure you are providing.

<html>

<FORM NAME="form1" METHOD="post" ACTION="go.php">

<INPUT TYPE="hidden" NAME="email" VALUE="info@client_url.com">
<INPUT TYPE="hidden" NAME="url" VALUE="www.client_url.com">
<INPUT TYPE="submit" NAME="Submit" VALUE="View the Client Website">
<INPUT TYPE="hidden" NAME="dr" VALUE="client">

</FORM>

</html>

--------

(go.php)

<?php
$url=$_POST['url'];
$email=$_POST['email'];
$dir=$_POST['dr'];

//connect to the database
$db = @mysql_connect("host","user""password");
if( ! ($db = @mysql_connect("host","user""password")) ) {
echo "Database Error";
exit;
}

//store the update
mysql_select_db("database",$db) or die("Log error");
$query="SELECT * FROM table WHERE dir = '$dir'";
$result = mysql_query( $query, $db );
$num_rows = mysql_num_rows($result);

//if new record
if (empty($num_rows)) {
$q1 = " INSERT INTO table (hits,url,email,dir) VALUES ('1','$url','$email','$dir')";
mysql_query($q1) or die("$q1 failed because ".mysql_error());

} else {

//if updating existing record
if ($result) {
$q2 = "UPDATE tracking SET hits=(hits+1), url='$url', email='$email' WHERE dir='$dir'";
mysql_query($q2) or die("$insert failed because ".mysql_error());
}
}

//and now to redirect the web browser ..
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Pragma: no-cache");
header ("Cache-Control: no-cache, must-revalidate");
header ("Location: http://$url");
exit;

?>

$now
"; echo "
$nowm

"; ?>

Back

Source