Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 132,689 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,299 people online right now. Registration is fast and FREE... Join Now!




preg_match

 
Reply to this topicStart new topic

preg_match, Trying to match first ???.??? to the first ???.??? in banlist

Addiction2Code
post 6 Jun, 2008 - 09:05 AM
Post #1


New D.I.C Head

*
Joined: 19 Dec, 2006
Posts: 39


My Contributions


Hello, I am having a problem with my site, I'm getting tons of ratings from crawl bots, "visiting the star links", as you know these are not accurate ratings... I am trying to implement a blacklist, this blacklist checks your IP with the IPs in the blacklist, and if its not in there, it lets you rate. Well, the crawl bots IP changes and there are more then one, making for a big blacklist that needs to be update much to often. So, what I wanted to do was make it so it matched the first two sections of the IP address with all the IPs in the blacklist, this way, I don't have to update them "as often, if ever". If come into some problems, and I have no clue where to go or what to do... I pieced this much together...

CODE

            $ip = getenv("REMOTE_ADDR");
            $banlist = mysql_query('SELECT * FROM blacklist');

            $ban = "False";
            while($row = mysql_fetch_array($banlist))
            {

                $pattern = '/(d+).(d+)./';
                preg_match($pattern, $ip, $matches, PREG_OFFSET_CAPTURE);

                if(count($matches) > 0)
                {
                    //don't toutch ban
                }
                else { $ban = "True"; }
            }


I have no clue if I'm even on the right track. All I want is some code that can match the first two sections of a full ip with the first two sections of another ip and return true if its a match and false if it is not
Thank you! All Suggestions Welcome!

This post has been edited by Addiction2Code: 6 Jun, 2008 - 10:38 AM
User is offlineProfile CardPM

Go to the top of the page

silverblaze
post 6 Jun, 2008 - 11:02 AM
Post #2


D.I.C Head

**
Joined: 18 Jan, 2008
Posts: 51



Thanked 3 times
My Contributions


hello Addiction2Code,
i jst got a small sugestion for u. try the following code it might wrk.
n let me knw if it does. i jst scribbled it quiclky.

CODE

<?php

            $ip = $_SERVER['REMOTE_ADDR'];
            $banlist = mysql_query('SELECT * FROM blacklist');

            $ban = "False";
            while($row = mysql_fetch_array($banlist))
            {

                $current_ip = $row[ip]; // if ur column name is ip in table blacklist
                
                // this will check the first 2 parts of the ip and returns true if bth of them are same

                if(substr($ip,0,strpos($ip,'.',4)+1) == substr($current_ip,0,strpos($current,'.',4)+1)){
                    $ban = "True";
                }
                else{
                    // dont touch ban
                }
            }

?>



i rite the code assuming that the blacklist ip is having the complete ip nt just first two parts. in srtpos i gave the offset as 4 because we want to get the second occurance nd in an ip address the second occurance will only come after the 4 charactet. and the last +1 is to get that '.' too.
hope this will work.
if u have any doughts plz get bak to me.

takecare.

This post has been edited by silverblaze: 6 Jun, 2008 - 11:04 AM
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 6 Jun, 2008 - 11:05 AM
Post #3


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


Do you have specific bots you're trying to black list? You could create a database full of the IP address that you don't want to allow access and just check that it's not in there. That's how a lot of blackhat SEOs do their thing.

Here's a great list of known search engine bots.
User is offlineProfile CardPM

Go to the top of the page

Addiction2Code
post 6 Jun, 2008 - 11:27 AM
Post #4


New D.I.C Head

*
Joined: 19 Dec, 2006
Posts: 39


My Contributions


Ok, well the first method did not work, as I supposed, a lot of IPs have a different placement, its not always ???.???.???.???, anyway, the whole list think is way to much for me to enter in my database and check every single time someone goes to the site, so i think I will just enable it for members or I'll have it Request user input somehow. anyway, I thank you greatly for your input, now I have a better idea as to what to do in the future.
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 6 Jun, 2008 - 11:46 AM
Post #5


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


Oh by the way, you shouldn't have to do a preg match. Just get the first two groups of digits and store them in a variable. Then run the query on your database.

Lets say you stored the first two groups in $ip.

$ip contains 312.53

You could do a mysql query that looks like

$sql = "select * from banned_ips where ip LIKE '$ip'%";

That would select anything that starts with those ip digits.

Hope that helps.
User is offlineProfile CardPM

Go to the top of the page

JBrace1990
post 6 Jun, 2008 - 11:49 AM
Post #6


D.I.C Regular

Group Icon
Joined: 9 Mar, 2008
Posts: 474



Thanked 21 times

Dream Kudos: 350
My Contributions


what akozlik said... I check for the entire IP though (since you don't want to ban entire servers)...
User is offlineProfile CardPM

Go to the top of the page

akozlik
post 6 Jun, 2008 - 11:52 AM
Post #7


D.I.C Addict

Group Icon
Joined: 25 Feb, 2008
Posts: 596



Thanked 22 times

Dream Kudos: 750
My Contributions


QUOTE(JBrace1990 @ 6 Jun, 2008 - 03:49 PM) *

what akozlik said... I check for the entire IP though (since you don't want to ban entire servers)...


Yeah I agree you don't want to ban the whole server, but it looks like that might be what he was trying to do. His original example only pulled the first two groups, which is what I adjusted that sql statement to do. It really could be applied however he wants to limit it.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 07:32AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month