Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-11-2006, 05:33 AM   PM User | #1
graficus
New Coder

 
Join Date: Aug 2006
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
graficus is an unknown quantity at this point
Question display info depending on IP

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
__________________
I know that I know nothing
graficus is offline   Reply With Quote
Old 11-11-2006, 07:10 AM   PM User | #2
rafiki
Senior Coder

 
rafiki's Avatar
 
Join Date: Aug 2006
Location: Floating around somewhere...
Posts: 2,034
Thanks: 18
Thanked 42 Times in 42 Posts
rafiki will become famous soon enough
PHP Code:

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

__________________
Get Firefox Now

Last edited by rafiki; 11-11-2006 at 07:15 AM..
rafiki is offline   Reply With Quote
Old 11-11-2006, 11:22 AM   PM User | #3
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
Ofcourse it has to be this (seems like a small typo with the { of the else function
PHP Code:
$ip $_SERVER['REMOTE_ADDR'];
if (
$ip == "12.34.56.*" || "23.45.*.*")
{
// your page here for these address's
}
else{
// normal page here

arne2 is offline   Reply With Quote
Old 11-11-2006, 03:52 PM   PM User | #4
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,744
Thanks: 2
Thanked 255 Times in 247 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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.
CFMaBiSmAd is online now   Reply With Quote
Old 11-11-2006, 04:21 PM   PM User | #5
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
Yes ofcourse, missed those errors
This compares correctly, except for the wildcard
PHP Code:
$ip $_SERVER['REMOTE_ADDR']; 
if (
$ip == "12.34.56.*" || $ip=="23.45.*.*"

// your page here for these address's 

else{ 
// normal page here 

arne2 is offline   Reply With Quote
Old 11-11-2006, 04:24 PM   PM User | #6
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,744
Thanks: 2
Thanked 255 Times in 247 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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.
CFMaBiSmAd is online now   Reply With Quote
Old 11-11-2006, 04:26 PM   PM User | #7
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
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:
PHP 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!

Last edited by arne2; 11-11-2006 at 04:31 PM..
arne2 is offline   Reply With Quote
Old 11-11-2006, 04:29 PM   PM User | #8
arne2
Regular Coder

 
Join Date: Aug 2006
Posts: 181
Thanks: 0
Thanked 0 Times in 0 Posts
arne2 is an unknown quantity at this point
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!
arne2 is offline   Reply With Quote
Old 11-11-2006, 05:02 PM   PM User | #9
CFMaBiSmAd
Senior Coder

 
CFMaBiSmAd's Avatar
 
Join Date: Oct 2006
Location: Denver, Colorado USA
Posts: 2,744
Thanks: 2
Thanked 255 Times in 247 Posts
CFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the roughCFMaBiSmAd is a jewel in the rough
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.
CFMaBiSmAd is online now   Reply With Quote
Old 11-12-2006, 05:15 PM   PM User | #10
graficus
New Coder

 
Join Date: Aug 2006
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
graficus is an unknown quantity at this point
Thank you all so very much!
Great scripts and ideas!


PS: everything works just the way I wanted
__________________
I know that I know nothing

Last edited by graficus; 11-12-2006 at 05:22 PM..
graficus is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:49 AM.


Advertisement
Log in to turn off these ads.