Jump to content

better way for referers using preg_replace


scarhand

Recommended Posts

Can any gurus think of a better way to write this?

 

<?php

$referer = $_SERVER['HTTP_REFERER'];
$preg_referer = preg_replace("/(http:\/\/(.*)\/)[\S]*/", "\\1", $referer);

if (!empty($referer) && $preg_referer != "http://www.mysite.com/" && $preg_referer != "http://mysite.com/")
{
  $sql = mysql_query("SELECT * FROM referer WHERE url='$referer'");
  $sql_count = mysql_num_rows($sql);
  
  if ($sql_count != 0) 
  {
    mysql_query("UPDATE referer SET hits=hits+1 WHERE url='$referer'");
  }
  else 
  {
    mysql_query("INSERT INTO referer (url, hits) VALUES ('$referer', '1')");
  }
}

?>

 

basically all i want it to do is update/insert the referer into the DB as long as it doesnt come from my own URL. this works but I am wondering if there is a better way of doing it.

i have made some improvements:

 

<?php

$ref = $_SERVER['HTTP_REFERER'];
$url = explode("/",$ref);
$surl = $url[2];
$found = ereg('\www.',$surl);

if ($found) 
$url = $surl;
else 
$url = "www.$surl";

if (!empty($surl) && $url != "www.mysite.com")
{
  $sql = mysql_query("SELECT * FROM referer WHERE url='$ref'");
  $sql_count = mysql_num_rows($sql);

  if ($sql_count == 1)
  {
    mysql_query("UPDATE referer SET hits=hits+1 WHERE url='$ref'");
  } 
  else 
  {
    mysql_query("INSERT INTO referer (url, hits) VALUES ('$ref', '1')");
  }
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.