Jump to content

blacklisting referers with php


yosuga

Recommended Posts

Hello, i want to blacklist visitors who come from some specific sites, i've hear i can do that from a .htaccess but i want to do that in PHP.

I've do this code for the moment but seem my array have a problem with strpos, any idea ?

<?php
$referer = $_SERVER['HTTP_REFERER'];
$badsites = array('google.com', 'example.com', 'examples2.com');
$pos = strpos($referer, $badsites);
if ($pos) {header('Location:blocked.php');}
?>

Warning: strpos() [function.strpos]: needle is not a string or an integer in /www/index.php on line 4

 

Regards

Edited by yosuga
Link to comment
Share on other sites

oh just for information i solved it like this:

<?php
function strposa($haystack, $needle, $offset=0) {
    if(!is_array($needle)) $needle = array($needle);
    foreach($needle as $query) {
        if(strpos($haystack, $query, $offset) !== false) return true; // stop on first true result
    }
    return false;
}
$referer = $_SERVER['HTTP_REFERER'];
$array = array('google.com', 'example2.com', 'example3.com');
$locka = strposa($referer, $array);
if($locka) {
header('Location: blocked.php');
} else {
//echo 'test';
}
?>
Link to comment
Share on other sites

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.