WEB [PHP/SQL Script] Awesome referer detection library.

Discussion in 'OT Technology' started by Jesse, Apr 20, 2008.

  1. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    Quick example:
    [​IMG]

    What it does is shows your most recent 5 referers (you can change this #) and/or your most popular referers.

    Can filter out your own domains, or search engines, or sites you don't want to have linked on your site. (in my case, 2girls1cup, etc)

    Can download here:
    http://www.findmysoft.com/scripts/refererLib-php-download.html

    google for refererLib.php to grab it.

    Or, right here:
    Code:
    <?php
    
    //problems? suggestions? email the author!
    //
    //              [email protected]
    
    //get most linked to pages on site
    //select count(visitURL) as count, visitURL from referer_visitLog group by visitURL order by count desc
    
    mysql_connect("dbHost", "dbUser", "dbPass");
    mysql_select_db("dbName");
    
    if ($refererList){
    	print "referers:<BR>";
    	$ar = refererList($refererList,"global");
    	print join("<BR>",$ar);
    }
    if ($topRefererList){
    	print join("<BR>",topRefererList($topRefererList,"global"));
    }
    
    function logReferer(){
    
    
    	$currentURL = $_SERVER['REQUEST_URI'];
    	$fullCurrentURL = "http://" . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    
    	$ref = getenv('HTTP_REFERER');
    
    	if (!$ref){
    		dbg("no referer");
    		return;
    	}
    
    	if ($ref != strip_tags($ref)){
    		//then they have tried something funny,
    		//putting HTML or PHP into the HTTP_REFERER
    		dbg("bad char in referer");
    		return;
    	}
    
    	$ignore = Array(
    		'your domain',
    		'http://www.myelin.co.nz/ecosystem/bot.php',
    		'http://radio.xmlstoragesystem.com/rcsPublic/',
    		'http://blogdex.media.mit.edu//',
    		'http://subhonker6.userland.com/rcsPublic/',
    		'mastadonte.com',
    
    	);
    	foreach ($ignore as $site){
    		if (stristr($ref, $site)){
    			dbg("referer ignored");
    			return;
    		}
    	}
    
    	$doubleCheckReferers = 0;
    
    	if ($doubleCheckReferers){
    
    		dbg("loading referering page");
    
    		//this is so that the page up until the call to
    		//logReferer will get shown before it tries to check
    		//back against the refering URL.
    		flush();
    
    		$goodReferer = 0;
    		$fp = @fopen ($ref, "r");
    		if ($fp){
    			//timeout after 5 seconds
    			socket_set_timeout($fp, 5);
    			while (!feof ($fp)) {
    				$page .= trim(fgets($fp));
    			}
    			if (strstr($page,$fullCurrentURL)){
    				dbg("found current url in page");
    				$goodReferer = 1;
    			}
    		}
    
    		if(!$goodReferer){
    			dbg("did not find \n\n:$fullCurrentURL:\n in \n\n\n :$page: \n\n\n");
    			return;
    		}
    
    	}
    
    
    
    	$anchor = preg_replace("/http:\/\//i", "", $ref);
    	$anchor = preg_replace("/^www\./i", "", $anchor);
    	$anchor = preg_replace("/\/.*/i", "", $anchor);
    
    	$sql ="insert into referer_visitLog (referingURL,baseDomain,visitURL) values ('$ref','$anchor','$currentURL')";
    
    	//print $sql;
    
    	mysql_query($sql);
    
    }
    
    
    
    function refererList ($howMany=5,$visitURL=""){
    
    	$i=2;
    
    	$ret = Array();
    
    	//if no visitURL, will show links to current page.
    	//if url given, will show links to that page.
    	//if url="global" will show links to all pages
    	if (!$visitURL){
    
    		$visitURL = $_SERVER['REQUEST_URI'];
    
    	}
    
    	if ($visitURL == "global"){
    		$sqr_recentReferer = mysql_query("select * from referer_visitLog order by visitID desc");
    	}
    	else {
    		$sqr_recentReferer = mysql_query("select * from referer_visitLog where visitURL = '$visitURL' order by visitID desc");
    	}
    
    
    
    	while($result_row = mysql_fetch_array($sqr_recentReferer)){
    
    		$fullUrl = $result_row['referingURL'];
    		$domain = $result_row['baseDomain'];
    		if (!$domain){
    			continue;
    		}
    
    		if ($last[$domain]){
    			continue;
    		}
    		$last[$domain] = 1;
    
    
    		$temp = "<a href=\"$fullUrl\" target=\"_blank\">$domain</a>";
    		array_push($ret,$temp);
    
    		if ($i++ > $howMany){
    			break;
    		}
    
    	}
    	return $ret;
    }
    
    
    function topRefererList ($howMany=5,$visitURL=""){
    
    
    	$i=2;
    
    	$ret = Array();
    
    
    	//see refererList() for notes.
    	if (!$visitURL){
    		$visitURL = $_SERVER['REQUEST_URI'];
    	}
    
    	if ($visitURL == "global"){
    		$sqr_recentReferer = mysql_query("select Count(referer_visitLog.baseDomain) as totalHits, referer_visitLog.baseDomain from referer_visitLog group by referer_visitLog.baseDomain order by totalHits desc limit $howMany");
    	}
    	else {
    		$sqr_recentReferer = mysql_query("select Count(referer_visitLog.baseDomain) as totalHits, referer_visitLog.baseDomain from referer_visitLog where visitURL = '$visitURL' group by referer_visitLog.baseDomain order by totalHits desc limit $howMany");
    	}
    
    	while($result_row = mysql_fetch_array($sqr_recentReferer)){
    
    		$count = $result_row['totalHits'];
    		$domain = $result_row['baseDomain'];
    
    		$uSet = mysql_query("select * from referer_visitLog where baseDomain = '$domain' order by visitID desc");
    		$uRow = mysql_fetch_array($uSet);
    		$latestUrl = $uRow["referingURL"];
    
    		$temp = "<a href=\"$latestUrl\" target=\"_blank\">$domain</a> ($count)";
    		array_push($ret,$temp);
    
    		if ($i++ > $howMany){
    			break;
    		}
    
    	}
    	return $ret;
    }
    
    function dbg($string){
    	//print $string . "<BR>\n";
    }
    
    
    if ($createTable){
    	print "Creating table:<BR>";
    	mysql_query("
    		create table referer_visitLog (
    			visitID int auto_increment,
    			primary key (visitID),
    			visitTime timestamp,
    			visitURL char(250),
    			referingURL char(250),
    			baseDomain char(250)
    		)
    	") or print "could not create table, might it exist?";
    }
    
    
    
    
    /*
    
    Usage:
    
    You must include the library in order to use it. Issue the include statement once on each page in which you want to use this library, before you call any of the functions.  A typical include statement would be:
    
    include("refererLib.php");
    
    To log the referers visiting a given page, place this code on the page:
    
    logReferer();
    
    
    To show a list of 5 pages that link to the current page (ordered by most recent visit) place this code:
    
    $list = refererList(5);
    foreach ($list as $link){
    	print "$link<BR>";
    }
    
    To show a list of the outside links most commonly used to get to the current page:
    
    $list = topRefererList(5);
    foreach ($list as $link){
    	print "$link<BR>";
    }
    
    In both cases, you can ask for a global list, i.e. a list of recent or top referers for all pages on your site that log referers:
    
    $list = refererList(5,"global");
    foreach ($list as $link){
    	print "$link<BR>";
    }
    
    Or:
    
    $list = topRefererList(5,"global");
    foreach ($list as $link){
    	print "$link<BR>";
    }
    
    */
    ?>
    Usage:
    Code:
    Usage:
    
    You must include the library in order to use it. Issue the include statement once on each page in which you want to use this library, before you call any of the functions.  A typical include statement would be:
    
    include("refererLib.php");
    
    To log the referers visiting a given page, place this code on the page:
    
    logReferer();
    
    
    To show a list of 5 pages that link to the current page (ordered by most recent visit) place this code:
    
    $list = refererList(5);
    foreach ($list as $link){
    	print "$link<BR>";
    }
    
    To show a list of the outside links most commonly used to get to the current page:
    
    $list = topRefererList(5);
    foreach ($list as $link){
    	print "$link<BR>";
    }
    
    In both cases, you can ask for a global list, i.e. a list of recent or top referers for all pages on your site that log referers:
    
    $list = refererList(5,"global");
    foreach ($list as $link){
    	print "$link<BR>";
    }
    
    Or:
    
    $list = topRefererList(5,"global");
    foreach ($list as $link){
    	print "$link<BR>";
    }
    
    */
    Make your SQL table:
    Code:
    create table referer_visitLog (
    			visitID int auto_increment,
    			primary key (visitID),
    			visitTime timestamp,
    			visitURL char(250),
    			referingURL char(250),
    			baseDomain char(250)
    		)
     
  2. 2ofdem

    2ofdem OT Supporter

    Joined:
    Jun 17, 2003
    Messages:
    114,854
    Likes Received:
    8
    Location:
    aus.vic.mel
    Nice :coold: Im gonna steal some of that code for my new affiliate system
     
  3. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    [​IMG]
     
  4. Ricky

    Ricky █▄ █▄█ █▄ ▀█▄

    Joined:
    Jun 17, 2005
    Messages:
    38,766
    Likes Received:
    6
    that's pretty fucking awesome. thanks
     
  5. Browning

    Browning Active Member

    Joined:
    Feb 14, 2005
    Messages:
    89,463
    Likes Received:
    10
    what are you having trouble with?
     
  6. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    Probably this part : logReferer();
     
  7. Browning

    Browning Active Member

    Joined:
    Feb 14, 2005
    Messages:
    89,463
    Likes Received:
    10
    :dunno: by the way thanks for posting this. I put it up on OTmp3:bowdown:

    oh and throwing a couple affiliate links just above=:cool:
     
  8. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    for shizzle.
     
  9. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    I'd place it in your footer. Doing so will make it track all pages.

    You could do it this way:
    Code:
    <?php
    logReferer();
    $list = topRefererList(5);
    foreach ($list as $link){
    	print "$link<BR>";
    }
    ?>
    or put this in the footer:
    Code:
    <?php
    logReferer();
    ?>
    and this there you want the results to be displayed:
    Code:
    <?php
    $list = topRefererList(5);
    foreach ($list as $link){
    	print "$link<BR>";
    }
    ?>
    Just note, every instance of logReferer(); on one page will count as one hit. So if it is posted once in the footer, and once on the page, your hits will go up by 2 each time.
     
  10. Shampoo

    Shampoo Rinse & Repost

    Joined:
    May 5, 2004
    Messages:
    60,183
    Likes Received:
    0
    Location:
    California
    bump

    heres an admin page for the lazys. made it ninja quick so it's pretty dirty code but it works.

    Code:
    
    <form method="POST" action="<?php echo $_SERVER['PHP_SELF']?>">
    
    <?php
    mysql_connect("DBHOST", "DBUSER", "DBPASS");
    mysql_select_db("DBNAME");
    
    if($_POST['delete']) {
    
    $check = $_POST['del'];
    
    foreach($check as $dcheck) {
    $delete = "DELETE FROM referer_visitLog WHERE baseDomain = '$dcheck'";
    MYSQL_QUERY($delete);
    }
    
    echo "<b>Domain(s) deleted<BR /><BR /></b>";
    }
    
    $sql = mysql_query("select * from referer_visitLog order by visitID desc");
    while($d = MYSQL_FETCH_ARRAY($sql)) {
    $bdomain[] = $d['baseDomain'];
    }
    $results = array_unique($bdomain);
    foreach($results as $r) {
    ?>
    <input type="checkbox" name="del[]" value="<?php echo $r?>"> <?php echo $r?><BR />
    	<?php
    	}
    ?>
    
    <input type=submit name=delete value="Delete Selected Domain">
    </form>
    
    
     
  11. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    Cliffs:

    say someone becomes your #1 referer, and you don't want to delete each of their entries one by one in PHPMYADMIN - this script will do it all for you.

    You can then blacklist the domains in your library file.

    I asked my nigga 'poo to make it, and he did.
     
  12. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    budump
     
  13. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    ps, this fucked up mine so don't use it til 'poo fixes it.
     
  14. Browning

    Browning Active Member

    Joined:
    Feb 14, 2005
    Messages:
    89,463
    Likes Received:
    10
    can't this to work now. New layout has a left and right sidebar. Right sidebar displays popular and random searches. When I put the referer in the left sidebar it works but it fucks the right sidebar.

    Here's the error it throws in the right sidebar

    Code:
    [B]Warning[/B]:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in [B]/home/username/public_html/otmp3/rightbar.php[/B] on line [B]10[/B]
    same for line 15

    here are lines 11-15
    Code:
        $row = mysql_num_rows($result); 
    
    
    
    
    while ($row = mysql_fetch_array($result)) {
     
  15. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    Would have to see the full code.
     
  16. Browning

    Browning Active Member

    Joined:
    Feb 14, 2005
    Messages:
    89,463
    Likes Received:
    10
    from the sidebar that is giving the error?

    Code:
        <div id="right_bar"> 
    
                    <div class="rightbox" style="text-align: left;">
      <span style="font-weight: bold; text-decoration: underline;">Popular Searches (<?=$typex?>)</span><br>
     
    <?php 
      
       $result = mysql_query("SELECT * FROM lyrics_tags ORDER BY count DESC LIMIT 11"); 
    
        $row = mysql_num_rows($result); 
    
    
    
    
    while ($row = mysql_fetch_array($result)) {
    
            $short = urldecode($row['tag_name']); // set variable
            $varlength = strlen($short); // count number of characters
            $limit = 20; // character limit
    
                if ($varlength > $limit) {
    
            $short = substr($short,0,$limit)."...";
    
    
                }
    
    echo  " <span style=\"padding-left: 5px;\"> <a href=\"".$CFG['domain']."/index.php?search=".$row['tag_name']."&type=$type\" title=\"".urldecode($row['tag_name'])." $type\">".$short."</a></span><br>\n\n";
    }
      ?>
      
                      </div>
                      
    
                    
                    <div class="rightbox" style="text-align: left; overflow: hidden">
      <span style="font-weight: bold; text-decoration: underline;">Random Searches (<?=$typex?>)</span><br>
     
    <?php 
      
       $result = mysql_query("SELECT * FROM lyrics_tags ORDER BY RAND() LIMIT 11"); 
    
        $row = mysql_num_rows($result); 
    
    while ($row = mysql_fetch_array($result)) {
    
            $short = urldecode($row['tag_name']); // set variable
            $varlength = strlen($short); // count number of characters
            $limit = 20; // character limit
    
                if ($varlength > $limit) {
    
            $short = substr($short,0,$limit)."...";
    
    
                }
    
    echo  " <span style=\"padding-left: 5px;\"> <a href=\"".$CFG['domain']."/index.php?search=".$row['tag_name']."&type=$type\" title=\"".urldecode($row['tag_name'])." Lyrics\">".$short."</a></span><br>\n\n";
    }
      ?>
                      </div>
    
        </div> 
     
  17. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    Well, I would personally use includes for the library file. ie:

    Code:
    <?php
    include(./refLibrary.php);
    $list = refererList(5);
    foreach ($list as $link){
    	print "$link<BR>";
    }
    ?>
    <?php
    include(./refLibrary.php);
    $list = topRefererList(5);
    foreach ($list as $link){
    	print "$link<BR>";
    }
    ?>
     
  18. Browning

    Browning Active Member

    Joined:
    Feb 14, 2005
    Messages:
    89,463
    Likes Received:
    10
    shit isn't working:wtc:
     
  19. waffler

    waffler Errrbuddy in this bitch get tipsy

    Joined:
    Jun 30, 2002
    Messages:
    1,148
    Likes Received:
    0
    Location:
    winnipeg
    cause otmp3.com sucks thats why
     
  20. Browning

    Browning Active Member

    Joined:
    Feb 14, 2005
    Messages:
    89,463
    Likes Received:
    10
  21. Browning

    Browning Active Member

    Joined:
    Feb 14, 2005
    Messages:
    89,463
    Likes Received:
    10
    it suck so much you went and copied it word for word:bowrofl:

    fail
     
  22. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    Waffler, Browning, and Ricky.

    Who wants to join the crew next? :hsugh:
     
  23. Browning

    Browning Active Member

    Joined:
    Feb 14, 2005
    Messages:
    89,463
    Likes Received:
    10
  24. Jesse

    Jesse PSN: iamajesse; XBL: Inhale My Rod; G8 GT crew; Ne OT Supporter

    Joined:
    Jan 12, 2005
    Messages:
    25,649
    Likes Received:
    0
    Location:
    California :: (925)
    [​IMG]
     
  25. Browning

    Browning Active Member

    Joined:
    Feb 14, 2005
    Messages:
    89,463
    Likes Received:
    10
    any more ideas about this error? :hsd:
     

Share This Page