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

Link to comment
https://forums.phpfreaks.com/topic/287947-blacklisting-referers-with-php/
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';
}
?>

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.