SchweppesAle Posted September 30, 2009 Share Posted September 30, 2009 Hi, I'm trying to use preg_match in order to compare a visiting user's IP to those within our database. The current format I'm using is pretty much just $IP = $row['IP']; preg_match("/$IP/", $User_IP); except it's run through a loop statement Anyway, it's returning the following error message, I figured it might be the periods within the IP String Warning: preg_match() [function.preg-match]: Unknown modifier My REGEX can be pretty weak Quote Link to comment https://forums.phpfreaks.com/topic/176069-preg_match-ip-address/ Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 "SELECT * FROM table WHERE `IP` = '{$IP}'" run that query you'll get results which have the same IP as $IP, however.. this is not a very good method to use to extend sessions, store the session id in its own field, then when a user logs in.. read his row's data, and start the session with the old id so that php doesn't start a completely new session its quite easy Quote Link to comment https://forums.phpfreaks.com/topic/176069-preg_match-ip-address/#findComment-927722 Share on other sites More sharing options...
SchweppesAle Posted September 30, 2009 Author Share Posted September 30, 2009 "SELECT * FROM table WHERE `IP` = '{$IP}'" run that query you'll get results which have the same IP as $IP, however.. this is not a very good method to use to extend sessions, store the session id in its own field, then when a user logs in.. read his row's data, and start the session with the old id so that php doesn't start a completely new session its quite easy That's actually a really good idea. Unfortunately, some of our IP fields have a few extra characters in them. Does this method look for an exact match or does it check substrings as well? Example "UA 192.168.1.1" I'd like to check and see if this string contains a sub-string "192.168.1.1" Thanks Quote Link to comment https://forums.phpfreaks.com/topic/176069-preg_match-ip-address/#findComment-927736 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 SELECT * FROM table WHERE `IP` LIKE '%{$IP}%' that will look for any `ip` field which contains the ip that is contained in $IP Quote Link to comment https://forums.phpfreaks.com/topic/176069-preg_match-ip-address/#findComment-927739 Share on other sites More sharing options...
SchweppesAle Posted September 30, 2009 Author Share Posted September 30, 2009 actual code $Bot_Test = FALSE; while($row = mysql_fetch_array($result_filter)) { $IP_UA = $row['IP/UA']; if((preg_match("/$IP_UA/", $ip_address) != 0) ||(preg_match("/$IP_UA/", $user_agent) != 0)) { $Bot_Test = TRUE; } } Quote Link to comment https://forums.phpfreaks.com/topic/176069-preg_match-ip-address/#findComment-927742 Share on other sites More sharing options...
SchweppesAle Posted September 30, 2009 Author Share Posted September 30, 2009 SELECT * FROM table WHERE `IP` LIKE '%{$IP}%' that will look for any `ip` field which contains the ip that is contained in $IP I was hoping to cache the resulting query too. That's why it "might" not be the best solution. Quote Link to comment https://forums.phpfreaks.com/topic/176069-preg_match-ip-address/#findComment-927745 Share on other sites More sharing options...
RussellReal Posted September 30, 2009 Share Posted September 30, 2009 $q = mysql_query("the select"); while ($row = mysql_fetch_assoc($q)) { // cache or w.e here $row will hold only rows with the ip that matches } Quote Link to comment https://forums.phpfreaks.com/topic/176069-preg_match-ip-address/#findComment-927774 Share on other sites More sharing options...
PFMaBiSmAd Posted September 30, 2009 Share Posted September 30, 2009 I was hoping to cache the resulting query too. Cache what part of it, for how long, and for what purpose? If you have hundreds of entries that are dynamically being added/deleted, you would be better off executing a query when needed. Quote Link to comment https://forums.phpfreaks.com/topic/176069-preg_match-ip-address/#findComment-927792 Share on other sites More sharing options...
SchweppesAle Posted September 30, 2009 Author Share Posted September 30, 2009 I was hoping to cache the resulting query too. Cache what part of it, for how long, and for what purpose? If you have hundreds of entries that are dynamically being added/deleted, you would be better off executing a query when needed. I have a list of IP and User agents stored within our database. The data is unchanging unless we decide to make an additional entry. Rather than query the database every time a page is requested I thought that I could improve performance by caching the results. require_once("flex_extension.php"); $user_agent = $_SERVER['HTTP_USER_AGENT']; $ip_address = $ip; $cache = & JFactory::getCache('callback' ); $result_filter = $cache->get( array( 'flex_extension', 'query' )); //boolean value will represent whether or not the visitor is a bot or human $Bot_Test = FALSE; while($row = mysql_fetch_array($result_filter)) { $IP_UA = $row['IP/UA']; if((preg_match("/$IP_UA/", $ip_address) != 0) ||(preg_match("/$IP_UA/", $user_agent) != 0)) { $Bot_Test = TRUE; } } flex_extension.php class flex_extension{ public static function query() { $query_filter = "SELECT * FROM jos_keyword_filter"; $result_filter = mysql_query($query_filter); return $result_filter; } } Quote Link to comment https://forums.phpfreaks.com/topic/176069-preg_match-ip-address/#findComment-927811 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.