si-blog

Pornographic referrals redux

Posted Apr 16, 2004 in PHP.

Following a suggestion from coubertin119, I have made changes to the script I wrote for denying unwanted referral data. Now I keep all the banned strings in an array, and loop through them with the very cool foreach() function. I even used the ternary operator ?, which I have always considered rather unwieldy, to good effect:

//check for banned or empty referer
($referer == "")?$update=0:$update=1;
$banned = array("jessey", "slamhost", "fistbang", "skipme", "episodesusdbz", "tartanweek", "shatteredreality", "porn", "amateurxpass", "nude", "anal", "ejaculation", "fuck", "pussy", "hand-job", "alaura-eden", "thebestofnet");
foreach($banned as $val) {
	if(stristr($referer,$val)) {
        	$update = 0;
        }
}
if($update == 1) {
	mysql_query($query, $link) or die ("Couldn't add referral data: " . mysql_error());
}

This is still only a temporary solution. In the future, I will automate the process by updating a table of banned referrers on my blog database, and then populating the array from that.

Once that is in place, I may consider extending it to handle unwanted user agents, like the StarProse Referrer Advertising System, for example.

Comments

  1. Gravatar

    $update = ($referer == "") ? 0 : 1;

    Posted by Anne on Apr 16, 2004.

  2. Gravatar

    I didn't realize that it could be simplified as much as that. I'll change it immediately. Thank you, Anne.

    Posted by Simon Jessey on Apr 16, 2004.

  3. Gravatar

    Why not use booleans? $update = ($referrer == "");

    And if it is blank, you don't want to check if it matches all the strings, do you?

    And if you find a matching string, you ban the referrer - you don't want to search all the rest to see if they match as well!

    Posted by Anonymous on Apr 16, 2004.

  4. Gravatar

    You are probably right on all counts, but my PHP is still very much in the novice phase. I guess I could break out of the loops with "break", when the conditions has been satisfied.

    Posted by Simon Jessey on Apr 16, 2004.