You Are Here: Home »   Code

Code Library - php

Last 5 Referers - (2,361 views)


<!-- create the following table in phpmyadmin -->

CREATE TABLE referers (
id INT (10) not null AUTO_INCREMENT,
site VARCHAR (255),
time INT (10),
PRIMARY KEY (id)
);


<!-- save this as refs.php and edit your database details in the top -->

<?

$db_host 
"localhost";
$db_username "username";
$db_password "password";
$db_name "database_name";

$ref $_SERVER['HTTP_REFERER'];
$domain explode("www."$_SERVER['SERVER_NAME']);
    
mysql_connect($db_host,$db_username,$db_password) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());

if (
$ref != "")
{
if (!
eregi($domain[1], $ref))
{

    
$time time();

    
$addref "INSERT INTO referers (site, time) VALUES ('$ref', '$time')";
    
mysql_query($addref);
  }
}

function 
show_refs()
{
   
$getref "SELECT * FROM referers ORDER BY time DESC LIMIT 5";
   
$reflist mysql_query($getref);

   echo 
"<b>Last 5 Referers:</b><br>\n";

   while(
$r=mysql_fetch_array($reflist))
{
   
$site $r['site'];

   echo 
"<a target=\"_blank\" href=\"$site\">$site</a><br>\n";
  }
}

?>


<!-- add this to the top of your homepage -->

<? include("refs.php"); ?>


<!-- then where you want to show the list use: -->

<? show_refs(); ?>