The problem is that when you explode()
your list of domain names, you end up with whitespace around each item. At the very least, you will have a newline (\n
) somewhere, since the linebreaks in your file are probably \r\n
.
So you're checking against something like " domain1.com"
or maybe "\ndomain1.com"
, or maybe "domain1.com\n"
. Since this extra whitespace doesn't exists in the referrer header, it's not matching when you expect it to.
By calling trim()
on each value you find, you'll get a clean domain name that you can use to do a more useful comparison:
$list = explode("\n", file_get_contents('list.txt'));
foreach ($list as $l) {
$l = trim($l);
if ((strlen($l) > 0) && (strpos($referer, $l) !== false)) {
$ref_found = true;
break;
}
}
I made a couple other minor updates to your code as well:
I switched away from using
chr()
and just used a string literal ("\n"
). As long as you use double-quotes, it'll be a literal newline character, instead of an actual\
andn
, and the string literal is much easier to understand for somebody reading your code.I switched from a
"\r"
character (chr 10) to a"\n"
character (chr 13). There's several different newline formats, but the most common are"\n"
and"\r\n"
. By exploding on"\n"
, your code will work with both formats, where"\r"
will only work with the second.I combined your two
if
statements. This is a very minor update that doesn't have much effect except to (in my opinion) make the code easier to read.I updated your
strpos()
to do a literal comparison to false (!==
). It's probably not an issue with this code because the referrer value will start withhttp://
, but it's a good habit to get into. If the substring happens to occur at the beginning of the parent string,strpos()
will return0
, which will be interpreted as false in your original code.I added a
break
statement in your loop if you found a matching domain name. Once you find one and set the flag, there's no reason to continue checking the rest of the domains in the list, andbreak
allows you to cancel the rest of theforeach
loop.