...

View Full Version : display info depending on IP



graficus
11-11-2006, 05:33 AM
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)

Thanks a million

rafiki
11-11-2006, 07:10 AM
$ip = $_SERVER['REMOTE_ADDR'];
if ($ip == "12.34.56.*" || "23.45.*.*")
{
// your page here for these address's
}
{
else
// normal page here
}

arne2
11-11-2006, 11:22 AM
Ofcourse it has to be this (seems like a small typo with the { of the else function

$ip = $_SERVER['REMOTE_ADDR'];
if ($ip == "12.34.56.*" || "23.45.*.*")
{
// your page here for these address's
}
else{
// normal page here
}

CFMaBiSmAd
11-11-2006, 03:52 PM
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.

arne2
11-11-2006, 04:21 PM
Yes ofcourse, missed those errors
This compares correctly, except for the wildcard


$ip = $_SERVER['REMOTE_ADDR'];
if ($ip == "12.34.56.*" || $ip=="23.45.*.*")
{
// your page here for these address's
}
else{
// normal page here
}

CFMaBiSmAd
11-11-2006, 04:24 PM
The following tested code will work for the IP address ranges you listed -

<?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.

arne2
11-11-2006, 04:26 PM
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

Now the code:

<?
function checkip($ip1){
$ip2 = '127.*.*.*';
$numbers1 = explode ('.', $ip1);
$numbers2 = explode ('.', $ip2);
for ($i = 0; $i < 4; $i++) {
if (($numbers1[$i] == $numbers2[$i]) or
($numbers1[$i] == '*') or
($numbers1[$i] == '*')) {
$match = true;
} else {
$match = false;
break;
}
return $match;
}


$ip = $_SERVER['REMOTE_ADDR'];
function checkip($ip);

if ($match)
{
// Your page for these adresses here
}
else{
//normal page here
}
?>

Note that i haven't tested it so it could have errors!

arne2
11-11-2006, 04:29 PM
Lol, didn't knew CFma was also typing a solution (which is better anyway i think).
Therefore, just use his instead of mine !



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!

CFMaBiSmAd
11-11-2006, 05:02 PM
The following array based code also works and allows you to easily modify the list of banned IP addresses -

<?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";
?>

graficus
11-12-2006, 05:15 PM
Thank you all so very much!
Great scripts and ideas!


PS: everything works just the way I wanted :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum