Showing the top domain referrals to your site
This tutorial will show you how to make a list of the top referring domains to your site. This list requires no signup from users at all, they just have to link to your site and they will show up. We will go through the basic creating the mysql tables in this tutorial and how to parse URLs into just the domain.
So first we set up the mysql table, we only need one, we will call it ref_domains with the following fields:
domainID - Primary,bigint, and autoincrement
domainname - varchar of length 255
hitsin - bigint
domainID is just an autogenerated ID that increments so there will not be two domains with the same ID
domainname is the actual url of the domain
hitsin is the number of hits that domain was given your site
Now we go on to the code:
<?php
$db = mysql_connect(”localhost”, “username”, “password”) or die(”Could not connect.”);
if(!$db)
die(”no db”);
if(!mysql_select_db(”database_name”,$db))
die(”No database selected.”);
$ref = getenv(”HTTP_REFERER”); //gets referrer
$dom=explode(”/”,$ref);
$domainstr=array($dom[0],$dom[1],$dom[2]);
$domainstring=implode(”/”,$domainstr); //builds just the domain
if(strlen($domainstring)>5) //make sure the domain referrer isn’t blank
{
$isref=”SELECT domainID from ref_domains where domainname=’$domainstring’”; //gets the ID of the domain and checks to see if it exists
$isref=mysql_query($isref) or die(”Could not query”);
$numrows=mysql_num_rows($isref); //count number of rows
if($numrows==0) //the referring domain is not in db already
{
$newref=”INSERT into ref_domains (domainname,hitsin) values(’$domainstring’,'1′)”;
mysql_query($newref) or die(”Could not insert new domain”);
}
else //increment hit by 1
{
$updatedomain=”Update ref_domains set hitsin=hitsin+1 where domainname=’$domainstring’”;
mysql_query($updatedomain) or die(”Could not update domain”);
}
}
//now we display them
$getdomains=”SELECT * from ref_domains order by hitsin DESC”;
$getdomains2=mysql_query($getdomains) or die(”Could not get domains”);
print “<table border=’1′><tr><td>Referrer</td><td>Hits in</td></tr>”;
while($getdomains3=mysql_fetch_array($getdomains2))
{
print “<tr><td><A href=’$getdomains3[domainname]‘>$getdomains3[domainname]</a></td><td>$getdomains3[hitsin]</td></tr>”;
}
print “</table>”;
?>
Lets go through it slowly. The first part:
$db = mysql_connect(”localhost”, “username”, “password”) or die(”Could not connect.”);
if(!$db)
die(”no db”);
if(!mysql_select_db(”database_name”,$db))
die(”No database selected.”);
Is just some basic connections to mySQL functions. Simply put your mysql username, password, and mysql database name where indicated. These are basic connection functions/code and can be used in any script with PHP/MYSQL.
$ref = getenv(”HTTP_REFERER”); //gets referrer
$dom=explode(”/”,$ref);
$domainstr=array($dom[0],$dom[1],$dom[2]);
$domainstring=implode(”/”,$domainstr); //builds just the domain
This gets the referring URL and extracts the domain from it. First getenv(”HTTP_REFERER”) gets the referring url. The next line with explode we separate the
URL into an array, based on where the “/” is. The part if the URL before the first “/” is $dom[0], between the first and second “/” is $dom[1] and so on. Next, we create a variable $domainstr and have it be an array of just the first three pieces of our domain array. Why? Because the first three pieces will always contain the domain name only. In a URL “http:” always comes before the first “/”, between the first and second “/” , there is nothing so it is empty and the www.whatever.com is always between the 2nd and 3rd “/”. Therefore imploding this $domainstr togehter with the “/” as the separator in the next line will always give us the full domain.
Now that we’ve extracted the root domain from the URL, we need to determine if that domain is already in our database or if it is a new referrer and we need code to handle each case, so we need the code below:
if(strlen($domainstring)>5) //make sure the domain referrer isn’t blank
{
$isref=”SELECT domainID from ref_domains where domainname=’$domainstring’”; //gets the ID of the domain and checks to see if it exists
$isref=mysql_query($isref) or die(”Could not query”);
$numrows=mysql_num_rows($isref); //count number of rows
if($numrows==0) //the referring domain is not in db already
{
$newref=”INSERT into ref_domains (domainname,hitsin) values(’$domainstring’,'1′)”;
mysql_query($newref) or die(”Could not insert new domain”);
}
else //increment hit by 1
{
$updatedomain=”Update ref_domains set hitsin=hitsin+1 where domainname=’$domainstring’”;
mysql_query($updatedomain) or die(”Could not update domain”);
}
}
So first we check if the referring domain is at least 5 characters, since a domain must have at least “http:” in it, it has to have at least 5 characters or it is not a valid referring domain. If it is, then we select the ID’s of the records where the URL is the domain that referred us. It counts the number of rows with that URL(basically it sees if the URL is already in our table or not). If it isn’t, then it does an INSERT query to insert the new URL in and give the new entry a hitsin value of 1. If it detects that the URL is already in our database(the else case), the it does an UPDATE query and increments hitsin by 1.
Now the last step is to query and display the information:
$getdomains=”SELECT * from ref_domains order by hitsin DESC”;
$getdomains2=mysql_query($getdomains) or die(”Could not get domains”);
print “<table border=’1′><tr><td>Referrer</td><td>Hits in</td></tr>”;
while($getdomains3=mysql_fetch_array($getdomains2))
{
print “<tr><td><A href=’$getdomains3[domainname]‘>$getdomains3[domainname]</a></td><td>$getdomains3[hitsin]</td></tr>”;
}
print “</table>”;
This just selects all the information in the database and orders it by the number of hits sent in, in descending order. Then it throws the query into an array and loops through the array and displays a hyperlink URL to the referring domain and the number of hits the domain has sent in in table format.
Copyright @ Chipmunk 2007

- Creating an object oriented MySQL abstraction class
- Using PHP to get prices from Amazon.com
- Going to the Polls with PHP: Part 2 - Admin panel
- Get Google Adsense statistics by using PHP
- Going to the Polls with PHP: Part 1 - The frontside
- 8 Essential MySQL Queries
- Converting MS Access to MySQL
- Creating sortable lists with PHP and Ajax
- XML Support in Microsoft SQL SERVER
- Implementing SQL Server 2005 Query Notifications in C# Windows Application
- Easy image contour extraction
- How to use Photoshop to create Product Box?
- My fair lady butterfly picture effect
- Sparkling magical special effect
- Photo Blending With Masks
- How to do two-color jobs, add varnish plates, and other specialty printing inks
- “Updo” Tutorial
- Text In Stitches
- Fantastic Mountain Landscape
- Devil’s Eye
Login
Friends' Sites
Contact Us
Categories
- 3D
- ASP
- C#
- CSS
- Database
- Flash
- GIMP
- Hosting
- Illustrator
- Java
- Javascript
- Linux
- Photoshop
- PHP
- Web Design
- Windows

1,703 views
No comments

No comments
Jump to comment form | comments rss [?] | trackback uri [?]