Reply
top refferals script?
Old 03-24-2006, 05:28 PM top refferals script?
DJA
DJA's Avatar
Skilled Talker

Posts: 87
I just want a PHP script that displays the top 10 referers, like on this site (video may offend some people who like ebaums world) http://www.ebaumsworldsucks.com/ Does anyone know of one?
__________________
http://www.donkery.com
DJA is offline
Reply With Quote
View Public Profile Visit DJA's homepage!
 
When You Register, These Ads Go Away!
Old 03-25-2006, 12:31 AM Re: top refferals script?
Super Talker

Posts: 144
shouldn't be hard at all. create a table which holds: URI VARCHAR, NumRefers INT DEFAULT 1, and a timestamp if you like.

you can use this to create the table:

CREATE TABLE ReferingWebsites (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, URI VARCHAR(100) NOT NULL, NumRefers INT NOT NULL DEFAULT 1, time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP())

when the page loads, read super global $_SERVER["HTTP_REFERER"] and check to see if the referer is present. if it's not from your own site, parse the domain from it and log it into the database. If that domain is already in the database, update the NumRefers + 1.

Code:
<?
  // establish the mysql connection
  $db = mysql_connect($server, $username, $password) or die(mysql_error());
  mysql_select_db($database_name, $db) or die(mysql_error());
 
  // update the URI count or add the refering URI to the list
  if ($_SERVER["HTTP_REFERER"] != "") {
    $arr = parse_url($_SERVER["HTTP_REFERER"]);
    if ($arr["host"] != "www.myhost.com") {
      $checkURI = mysql_query("SELECT id, NumRefers FROM ReferingWebsites WHERE URI = '". $arr["host"] ."'", $db);
 
      if (mysql_num_rows($checkURI) > 0) {
        $row = mysql_fetch_array($checkURI);
        mysql_query("UPDATE ReferingWebsites SET NumRefers = ". (intval($row[1]) + 1) ." WHERE id = ". $row[0], $db);
      }
      else
        mysql_query("INSERT INTO ReferingWebsites (URI) VALUES ('". $arr["host"] ."')", $db);
    }
  }
 
  // get the top ten and loop through them to display in an ordered list
  $getTopTenRef = mysql_query("SELECT URI, NumRefers FROM ReferingWebsites ORDER BY NumRefers DESC LIMIT 10", $db);
 
  if (mysql_num_rows($getTopTenRef) > 0) {
    echo "<ol>";
    while ($row = mysql_fetch_array($getTopTenRef))
      echo "<li><a href=\"http://". $row[0] ."\" target=\"_blank\">". $row[0] ."</a> (". $row[1] .")</li>"
    echo "</ol>";
  }
 
  // close the mysql connection
  mysql_close($db);
?>
done deal.
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 03-25-2006, 12:41 AM Re: top refferals script?
Skilled Talker

Posts: 88
Referer.org
use this

its free and its a little javascript that tells you whos refering to your site
raptrex is offline
Reply With Quote
View Public Profile Visit raptrex's homepage!
 
Old 03-25-2006, 11:32 PM Re: top refferals script?
DJA
DJA's Avatar
Skilled Talker

Posts: 87
When I try to add this code into my index there was obviously an error because it just comes up with a blank page, I made the database and everthing and I tested tring to connect to the database and it works. What to do?

<?
$server = "(blocked)";
$username = "(blocked)";
$password = "(blocked)";
$database_name = "ReferingWebsites";
// establish the mysql connection
$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($database_name, $db) or die(mysql_error());

// update the URI count or add the refering URI to the list
if ($_SERVER["HTTP_REFERER"] != "") {
$arr = parse_url($_SERVER["HTTP_REFERER"]);
if ($arr["host"] != "www.donkery.com") {
$checkURI = mysql_query("SELECT id, NumRefers FROM ReferingWebsites WHERE URI = '". $arr["host"] ."'", $db);

if (mysql_num_rows($checkURI) > 0) {
$row = mysql_fetch_array($checkURI);
mysql_query("UPDATE ReferingWebsites SET NumRefers = ". (intval($row[1]) + 1) ." WHERE id = ". $row[0], $db);
}
else
mysql_query("INSERT INTO ReferingWebsites (URI) VALUES ('". $arr["host"] ."')", $db);
}
}

// get the top ten and loop through them to display in an ordered list
$getTopTenRef = mysql_query("SELECT URI, NumRefers FROM ReferingWebsites ORDER BY NumRefers DESC LIMIT 10", $db);

if (mysql_num_rows($getTopTenRef) > 0) {
echo "<ol>";
while ($row = mysql_fetch_array($getTopTenRef))
echo "<li><a href=\"http://". $row[0] ."\" target=\"_blank\">". $row[0] ."</a> (". $row[1] .")</li>"
echo "</ol>";
}

// close the mysql connection
mysql_close($db);
?>
__________________
http://www.donkery.com
DJA is offline
Reply With Quote
View Public Profile Visit DJA's homepage!
 
Old 03-25-2006, 11:41 PM Re: top refferals script?
Super Talker

Posts: 144
sorry, i missed a semicolon. typed it too fast

line 27,

echo "<li><a href=\"http://". $row[0] ."\" target=\"_blank\">". $row[0] ."</a> (". $row[1] .")</li>";

make sure there's a semicolon at the end of that
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 03-25-2006, 11:45 PM Re: top refferals script?
Super Talker

Posts: 144
hmm, I also notice that your $database_name is that of your table name. understand that these are different. they can be the same name, if thats what you intended... for testing purposes or whatnot, but you should be providing the name of your database in $database_name. other than that, i've just tested it and it's working on my end.
__________________
create.vibe
web development portfolio
createvibe.com is offline
Reply With Quote
View Public Profile Visit createvibe.com's homepage!
 
Old 03-26-2006, 08:21 AM Re: top refferals script?
DJA
DJA's Avatar
Skilled Talker

Posts: 87
I was just using it for the test, I deleted it and made another database. Its working now, thank you very much!
__________________
http://www.donkery.com
DJA is offline
Reply With Quote
View Public Profile Visit DJA's homepage!
 
Reply     « Reply to top refferals script?
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.16127 seconds with 12 queries