Jump to content

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')");
  }
}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.