28, May 2012

HOWTO:[PHP] referers - webmaster forum

 
Webdigity webmaster forums
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: HOWTO:[PHP] referers
« previous next »
Pages: [1] Print
Instabuck - The easy way to sell digital products online

Author Topic: HOWTO:[PHP] referers  (Read 9323 times)
OMG!I am geek
**
Posts: 53
434 credits
Members referred : 0


« on: Nov 04, 2005, 02:57:11 pm »

well, for those that dont know, i wrote a tutorial for referers of a site. It is a mysql database referer. The code comes with the comments and should be self explanitary

Code:
<?php
/*==================================
USER EDITS BELOW
==================================*/
$DB = array(
         
"host"   =>   "localhost"//Your host
         
"user"   =>   "",         //Sql User
         
"pass"   =>   "",         //Sql Password
         
"db"   =>   "",         #Sql Database Name
         
);
/*==================================
DONT EDIT BELOW UNLESS YOU NO WHAT YOUR DOING!
==================================*/
//connect to the database
$conn mysql_connect($DB["host"], $DB["user"], $DB["pass"]);
mysql_select_db($DB["db"], $conn) or die("Mysql Said:"mysql_error().":"mysql_errno());
//before we carry on lets make the table...
/*=====================================================
//NOTE : MUST DELETE THIS AFTER YOU HAVE RUN IT ONCE!\\
=====================================================*/
if(!mysql_query("CREATE TABLE `referers` (
  `id` int(11) NOT NULL auto_increment,
  `url` varchar(255) NOT NULL default '',
  `name` varchar(255) NOT NULL default '',
  `hits` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;"
)) {
   
//if we cant make it, display why we cant make it...
   
die("Mysql Said:"mysql_error().":"mysql_errno());
}
//DONT DELETE NO MORE
/*Use of this function?
checks to see if there is a referer
if there is check if it is in the db
if it isnt in the db insert it
if it is in the db update it*/
function updateref() {
   global 
$conn$DB;
   
//get the main link of the referer
   //http://test.com?page=test&trim=2
   //shortened to http://test.com
   
$referer preg_replace("/(http:\/\/(.*)\/)[\S]*/""\\1"$_SERVER['HTTP_REFERER']);
   
$name preg_replace("/http:\/\//"""$referer);
   
$name preg_replace("/\//"""$name);
   
   
//is there a referer?
   
   
if(!$referer) {
   
      
//no?! :O return false
      
return FALSE;
     
   }
   
   if(
$referer) {
   
      
//good there is a referer
      
$query mysql_query("SELECT * FROM referers WHERE url='$referer'"$conn);
     
      
//have we been refered from this link before?
     
      
if(mysql_num_rows($query) < 1) {
     
         
//no... insert him into the DB
         
         
mysql_query("INSERT INTO referers (url, name, hits) VALUES ('$referer', '$name', 1)"$conn);
     
      }
      else {
     
         
//yes he has, update the link
         
mysql_query("UPDATE referers SET hits=hits+1 WHERE url='$referer'"$conn);
         
      }
     
   }
}
/*Use of this function?
show the referers on your site
name, link, how many hits they have*/
function showrefs() {
   global 
$DB$conn;
   
//the query...
   //Select and order in descending order the hits they have
   
$query mysql_query("SELECT * FROM referers ORDER BY hits DESC LIMIT 10"$conn);
   while(
$ref mysql_fetch_array($query)) {
      
//echo the link, name and how many times they have been referered to ur site
      
echo "<a href=\"{$ref['url']}\">{$ref['name']} ({$ref['hits']})</a><br>";
   }
}
?>


when you save the file include it into your page and call the functions

Code:
<?php
include("referer.php");
updateref(); //updates refererer
?>


and

Code:
<?php
showrefs
();
?>


to show the referers on your site

just to make things simple and easy for people who have no idea what anything in php is, regulate the code a little

From:

Code:
<?php
/*==================================
USER EDITS BELOW
==================================*/
$DB = array(
         
"host"   =>   "localhost"//Your host
         
"user"   =>   "",         //Sql User
         
"pass"   =>   "",         //Sql Password
         
"db"   =>   "",         #Sql Database Name
         
);
/*==================================
DONT EDIT BELOW UNLESS YOU NO WHAT YOUR DOING!
==================================*/
//connect to the database
$conn mysql_connect($DB["host"], $DB["user"], $DB["pass"]);
mysql_select_db($DB["db"], $conn) or die("Mysql Said:" mysql_error().":"mysql_errno());
//before we carry on lets make the table...
/*=====================================================
//NOTE : MUST DELETE THIS AFTER YOU HAVE RUN IT ONCE!\\
=====================================================*/
if(!mysql_query("CREATE TABLE `referers` (
  `id` int(11) NOT NULL auto_increment,
  `url` varchar(255) NOT NULL default '',
  `name` varchar(255) NOT NULL default '',
  `hits` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;"
)) {
   
//if we cant make it, display why we cant make it...
   
die("Mysql Said:"mysql_error().":"mysql_errno());
}

To:

Code:
<?php
/*==================================
USER EDITS BELOW
==================================*/
$DB_host "localhost";//Your host
$DB_user "user";     //Sql User
$DB_pass "pass";     //Sql Password
$DB "db";            //Sql Database Name

/*==================================
DONT EDIT BELOW UNLESS YOU NO WHAT YOUR DOING!
==================================*/
//connect to the database
$conn mysql_connect($DB_host$DB_user$DB_pass);
mysql_select_db($DB$conn) or die("Mysql Said: ".mysql_error().":".mysql_errno());

Then save this as a new file:

Code:
<?php
/*=====================================================
//NOTE : RUN ONCE, THEN REMOVE THE FILE FROM YOUR SERVER\\
=====================================================*/
if(!mysql_query("CREATE TABLE `referers` (
  `id` int(11) NOT NULL auto_increment,
  `url` varchar(255) NOT NULL default '',
  `name` varchar(255) NOT NULL default '',
  `hits` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) TYPE=MyISAM;"
)) {
   
//if we cant make it, display why we cant make it...
   
die("Mysql Said: ".mysql_error().":".mysql_errno());
}

p.s. also fixed some possibly errors with the die()'s check to see if your's worked (im pretty sure the mission '.'s would cause an error)
« Last Edit: Nov 04, 2005, 03:30:48 pm by Nikolas »
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=652
Tags : php tutorials mysql databases Bookmark this thread : Digg Del.icio.us Dzone more....

Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: HOWTO:[PHP] referers
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 28, 2012, 10:24:41 pm





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!






Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2012 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.