Jump to content

preg_match IP Address


SchweppesAle

Recommended Posts

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  :wtf:

Link to comment
Share on other sites

"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

Link to comment
Share on other sites

"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

Link to comment
Share on other sites

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;
}


}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.