PHP Development Board php divider

User Options
Register--Login--Top 20 Posters--Search Topics


Forum Main>>Tutorials>>Showing the top domain referrals to your site.

New Topic-Reply




Author
Post
Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged

PM ID and RPS ID: 1
PM [Chipmunk]

View Member Photo

Last replied to on Thu Feb 25, 2010 00:12:04
Edit Post|Quote
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:

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:

Code:

$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.

Now, the next part of the code:

Code:

$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:

Code:

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:

Code:

$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.
-----------------------------
Chipmunk,
Supreme Administrator

stafygraph
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 24175
[PM stafygraph]

Posted at Wed Sep 02, 2009 04:07:24
Edit post|Quote
I need some help blocking all traffic from a particular domain referral, lets say xxx.com. I know this requires mod rewrite to first detect the referral itself and act accordingly, but don't know where to go from there. Can anyone help?The problem I'm facing is that starting recently, a sex related domain has been showing up big time in our referral logs. The referral URL itself is some strange cgi redirect script I can't make heads or tails from (redirects to a 404 page), and it's obvious they're not sending actual visitors to my site. I suspect they may simply be hot linking or accessing some non html files on our server, jacking up the referral logs in the process. Using mod rewrite would seem to be the only solution, as something like IP tables blocks the domain itself, but not traffic redirected from and not necessarily originating from the offending domain (referrals).
-----------------------------
auto insurance quotes - houses for sale - home mortgage

grow
Rank:acorn
Group: members
Posts: 8
IP Logged
PM ID and RPS ID: 26893
[PM grow]

RPS score: 0
RPS challenge

Posted at Thu Feb 04, 2010 08:53:22
Edit post|Quote
I am using the Showing the top domain referrals to your site script to check and monitor the main traffic of my site on daily basis.

grow
Rank:acorn
Group: members
Posts: 8
IP Logged
PM ID and RPS ID: 26893
[PM grow]

RPS score: 0
RPS challenge

Posted at Thu Feb 04, 2010 09:06:06
Edit post|Quote
Quote:
  Nice work


chilpil
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 26907
[PM chilpil]

RPS score: 0
RPS challenge

Posted at Fri Feb 05, 2010 05:09:20
Edit post|Quote
This is a simple tutorial that shows you how to make information or
images(in ase practice tests this case buttons) display in a specific number columns per
row using a specific algorithem and loop.First for this example, create a table in mysql called affiliates with the follwing fields: affiliateID -primary, auto-increment, bigint URL - varchar, a+ certification practice test length 255

button - varchar length 255

URL will store the URL of the site you want to link to, and button will how the URL of the button of that site.

Now you need a simple connector file like the following connect.php to connect to the database.83-640 exam 

grow
Rank:acorn
Group: members
Posts: 8
IP Logged
PM ID and RPS ID: 26893
[PM grow]

RPS score: 0
RPS challenge

Posted at Fri Feb 05, 2010 10:29:16
Edit post|Quote
Quote:
I was just wondering over the topic here and i found it very good and described very nicely. I would like to participate in your blog and will add some more value to it. 
I was also searching for some scripting site and found it really useful.Ohio State Jersey

grow
Rank:acorn
Group: members
Posts: 8
IP Logged
PM ID and RPS ID: 26893
[PM grow]

RPS score: 0
RPS challenge

Posted at Sat Feb 06, 2010 06:57:09
Edit post|Quote
Quote:
  [quote]  Nice work Done. Perth Insulation 
[/quote]

grow
Rank:acorn
Group: members
Posts: 8
IP Logged
PM ID and RPS ID: 26893
[PM grow]

RPS score: 0
RPS challenge

Posted at Sat Feb 06, 2010 10:51:11
Edit post|Quote
I was looking for some help about API to work in my some projects and i found very useful information on this blog. sports picks

cerse
Rank:acorn
Group: members
Posts: 5
IP Logged
PM ID and RPS ID: 27372
[PM cerse]

RPS score: 0
RPS challenge

Posted at Thu Feb 25, 2010 00:12:04
Edit post|Quote
This is how to paginate a long story/article by sentences. This
tutorial will teach you how to split large texts into arrays, get rid
of the parts not in the page, collapse the parts you want together and
finally paginate the parts.
So first I will show you the entire example code, this tutorial does not require any databases.a+ essentials practice test

Page: 1



Powered by © Chipmunk Board

Flash games Ninja games-Web Design New York