Need help with IP detecting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Chinclub
Forum Newbie
Posts: 13
Joined: Sun Aug 31, 2008 7:33 am

Need help with IP detecting

Post by Chinclub »

I have a pet classifieds script that I wrote in PHP and now that it is high on the search engines I am getting a ton of SPAM ads. I notice that most of the new members who post the spam come from IP addresses starting with either 119. or 122. I want to place a line of script in the register.php that will flag any new member coming from an IP address starting with those numbers. I can't seem to figure out how to do it with my limited PHP knowledge.

I was hoping something like would work:

Code: Select all

$ip = $_SERVER['REMOTE_ADDR'];
if ($ip = '119.%'){
print " Your account is flagged please contact the admin";}
else{
I tried it with my own IP address and it didn't work. Does anyone have a suggestion? I have to find a way to keep these people out of my site. Its becoming a huge problem :banghead:
samwho
Forum Newbie
Posts: 15
Joined: Sat Apr 03, 2010 8:06 am

Re: Need help with IP detecting

Post by samwho »

You could try:

Code: Select all

$ip = $_SERVER['REMOTE_ADDR'];
if (substr($ip,0,3) == '119' || substr($ip,0,3) == '122'){
print " Your account is flagged please contact the admin";}
else{
}
Also, don't know if you noticed, but your operator in your if statement was wrong. You used the assignment ("=") operator instead of the comparison ("==") operator :)
Chinclub
Forum Newbie
Posts: 13
Joined: Sun Aug 31, 2008 7:33 am

Re: Need help with IP detecting

Post by Chinclub »

That seems to work great!! Thanks.
samwho
Forum Newbie
Posts: 15
Joined: Sat Apr 03, 2010 8:06 am

Re: Need help with IP detecting

Post by samwho »

You need to be careful when basing ban systems on IP addresses, though, especially ones as general as blocking based on the first 3 digits... I don't know a huge amount about IP addresses but I can imagine blocking all addresses beginning with a certain 3 digits to be a little risky... Anyone able to give more insight into this? :)
Chinclub
Forum Newbie
Posts: 13
Joined: Sun Aug 31, 2008 7:33 am

Re: Need help with IP detecting

Post by Chinclub »

Do you mean by accidentally blocking valid visitors? The best I can tell from IP lookups it seems those first three digits specify a foreign country. Since my classifieds are targeting people in the USA and Canada I'm not concerned with this problem. However, I did plan on having a contact form for anyone who gets blocked by the IP catcher and still feels they want to join. Then I can create those accounts on a case by case basis. I am hoping these spam ad placers won't waste there time when they see I am serious about not placing their ads.

Is there another problem I am not thinking of? :dubious:
samwho
Forum Newbie
Posts: 15
Joined: Sat Apr 03, 2010 8:06 am

Re: Need help with IP detecting

Post by samwho »

No, that was my main concern :)
Post Reply