scarhand Posted July 28, 2008 Share Posted July 28, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/116914-better-way-for-referers-using-preg_replace/ Share on other sites More sharing options...
.josh Posted July 28, 2008 Share Posted July 28, 2008 it looks like your code checks whether your site has www. on it or not, but not any other ones, so your database could end up having at least http://www.somesite.com/ http://somesite.com/ in your table. Quote Link to comment https://forums.phpfreaks.com/topic/116914-better-way-for-referers-using-preg_replace/#findComment-601287 Share on other sites More sharing options...
scarhand Posted July 28, 2008 Author Share Posted July 28, 2008 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')"); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/116914-better-way-for-referers-using-preg_replace/#findComment-601326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.