Hi, I know next to nothing about PHP. One simple snippet I need.
In plain english something like this:
if ip other than "12.34.56.*" or "23.45.*.*"
then continue displaying page.
if not
then display "my text" and stop(or redirect)
(i need this to block spammers destroying my wordpress blog (or leaving me with hundreds of comments to moderate every day). there are only a few of them, so this seems like the best solution. before you suggest a different approach, please give pointers on the above)
The if(...) statement provided in the above code does not work for two reasons -
Firstly, you cannot OR || values together and then test them against a value.
Secondly, you cannot directly use a simple == test and get a wild card character * match to function.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
The following tested code will work for the IP address ranges you listed -
PHP Code:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
if(preg_match("#^(12\.34\.56|23\.45)\.#", $ip))
{
// found a match
echo "Bad IP"; // in the real code, don't echo anything, see the note following the code.
exit();
}
// did not find a match
echo "Continue the remainder of your normal code here";
?>
Note: In real life, you probably don't want to echo anything for a banned IP address. The person abusing your web site will simply go through a different IP address. If you simply do an exit(), they might think the site is broken and stop abusing it.
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.
maybe the code below helps you, but note that i'm a noob myself so it could be totally wrong as well!
As i don't want to claim code to be mine if it aint : i found (the biggest part of) the code at http://www.thescripts.com/forum/thread2146.html
Lol, didn't knew CFma was also typing a solution (which is better anyway i think).
Therefore, just use his instead of mine !
Quote:
Note: In real life, you probably don't want to echo anything for a banned IP address. The person abusing your web site will simply go through a different IP address. If you simply do an exit(), they might think the site is broken and stop abusing it.
=> My response to that: you can make it even better ! What about putting some mysql_errors (like couldn't connect to the database) or something in the echo, they will then think your site is broken. Or put some other errors. Or put in 'offline' or 'under construction' or something else. That might work even better than a white screen! Or redirect them to another website, like www.anonexistingsite.com. Then it really looks offline (you can type a 'we have moved to .. ' message in the echo as well) As you can see, loads of ideas!
The following array based code also works and allows you to easily modify the list of banned IP addresses -
PHP Code:
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$banned = array();
$banned[] = "12.34.56.";
$banned[] = "23.45.";
// add other IP's as needed
$found = FALSE;
foreach($banned as $value)
{
if(strpos($ip, $value) === 0){
// found a match starting in position 0
$found = TRUE;
break;
}
}
if($found)
{
// found a match
echo "Bad IP";
exit();
}
// did not find a match
echo "Continue the remainder of your normal code here";
?>
__________________
If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.