[PHP] Re: Matching *exact* string?

2004-10-31 Thread Per Jessen
Nick Wilson wrote:

 hello all
 
 I am foreach()ing through an array of ip addresses in a 'ban script' and
 have the following php code:
 
 foreach($ips as $ip) {
   preg_match(/$ip/, $_SERVER[REMOTE_ADDR]);
   $ban = TRUE;
 }
 
 This is great, but if 127.0.0 were in the ban list (for example) it
 would still produce a ban as it partially matches.
 
 How can I alter the above so that only *exact* matches are banned?

If I've understood your question right:

foreach($ips as $ip) {
  preg_match(/^$ip$/, $_SERVER[REMOTE_ADDR]);
  $ban = TRUE;
}



-- 
Per Jessen, Zurich
Let your spam stop here -- http://www.spamchek.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Matching *exact* string?

2004-10-31 Thread Aidan Lister
I cannot fathom why you would use preg_match for this.

This will get an exact match...
if ($ip == $_SERVER[REMOTE_ADDR]) { $ban = true; }

Despite this being the worst idea I've ever seen, combined with a true lack 
of understanding, I wish you well.


Nick Wilson [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 hello all

 I am foreach()ing through an array of ip addresses in a 'ban script' and
 have the following php code:

 foreach($ips as $ip) {
  preg_match(/$ip/, $_SERVER[REMOTE_ADDR]);
  $ban = TRUE;
 }

 This is great, but if 127.0.0 were in the ban list (for example) it
 would still produce a ban as it partially matches.

 How can I alter the above so that only *exact* matches are banned?

 Much thanks!
 -- 
 Nick W 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Re: Matching *exact* string?

2004-10-31 Thread Daniel Schierbeck
Nick Wilson wrote:
hello all
I am foreach()ing through an array of ip addresses in a 'ban script' and
have the following php code:
foreach($ips as $ip) {
  preg_match(/$ip/, $_SERVER[REMOTE_ADDR]);
  $ban = TRUE;
}
This is great, but if 127.0.0 were in the ban list (for example) it
would still produce a ban as it partially matches.
How can I alter the above so that only *exact* matches are banned?
Much thanks!
I'd rather go with something like this:
$banned_ips = array('123.123.123.123', '321.321.321.321'); // Banned IPs
if (in_array($_SERVER['REMOTE_ADDR'], $banned_ips)) {
die('Dude, you\'re banned!');
}
But if I were you I'd choose something more advanced.
--
Daniel Schierbeck
Help spread Firefox (www.getfirefox.com): 
http://www.spreadfirefox.com/?q=user/registerr=6584

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[PHP] Re: Matching *exact* string?

2004-10-31 Thread Daniel Schierbeck
Daniel Schierbeck wrote:
Nick Wilson wrote:
hello all
I am foreach()ing through an array of ip addresses in a 'ban script' and
have the following php code:
foreach($ips as $ip) {
  preg_match(/$ip/, $_SERVER[REMOTE_ADDR]);
  $ban = TRUE;
}
This is great, but if 127.0.0 were in the ban list (for example) it
would still produce a ban as it partially matches.
How can I alter the above so that only *exact* matches are banned?
Much thanks!

I'd rather go with something like this:
$banned_ips = array('123.123.123.123', '321.321.321.321'); // Banned IPs
if (in_array($_SERVER['REMOTE_ADDR'], $banned_ips)) {
die('Dude, you\'re banned!');
}
But if I were you I'd choose something more advanced.
And yes, I'm aware of the fact that 321.321.321.321 isn't a valid IP...
--
Daniel Schierbeck
Help spread Firefox (www.getfirefox.com): 
http://www.spreadfirefox.com/?q=user/registerr=6584

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Re: Matching *exact* string?

2004-10-31 Thread Nick Wilson

* and then Aidan Lister declared
 I cannot fathom why you would use preg_match for this.
 
 This will get an exact match...
 if ($ip == $_SERVER[REMOTE_ADDR]) { $ban = true; }

Ahh.. an oversight. The script has been cobbled together from a previous
version that supported partial matches. I'd just missed the obvious,
thankyou ;-)

 Despite this being the worst idea I've ever seen, combined with a true lack 
 of understanding, I wish you well.

There is no need to be insulting. I doubt very much anyone thinks your a
clever guy for acting like a twat on a mailing list.

-- 
Nick W

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Matching *exact* string?

2004-10-31 Thread Nick Wilson

* and then Per Jessen declared
 foreach($ips as $ip) {
   preg_match(/^$ip$/, $_SERVER[REMOTE_ADDR]);
   $ban = TRUE;
 }

Great! - found a more sutable way but that's appreciated. Cheers!

-- 
Nick W

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Matching *exact* string?

2004-10-31 Thread Nick Wilson

* and then Daniel Schierbeck declared
 I'd rather go with something like this:
 
 $banned_ips = array('123.123.123.123', '321.321.321.321'); // Banned IPs
 
 if (in_array($_SERVER['REMOTE_ADDR'], $banned_ips)) {
   die('Dude, you\'re banned!');
 }
 
 But if I were you I'd choose something more advanced.

I think in_array() sounds like a great solution, wonder if it's faster
than comparing each ooe with == ? thanks!

-- 
Nick W

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Matching *exact* string?

2004-10-31 Thread Greg Donald
On Sun, 31 Oct 2004 12:58:04 +0100, Nick Wilson [EMAIL PROTECTED] wrote:
 I think in_array() sounds like a great solution, wonder if it's faster
 than comparing each ooe with == ? thanks!

Benchmark it both ways and find out.  Then post back and tell us.  :)


-- 
Greg Donald
Zend Certified Engineer
http://gdconsultants.com/
http://destiney.com/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: Matching *exact* string?

2004-10-31 Thread Nick Wilson

* and then Greg Donald declared
 On Sun, 31 Oct 2004 12:58:04 +0100, Nick Wilson [EMAIL PROTECTED] wrote:
  I think in_array() sounds like a great solution, wonder if it's faster
  than comparing each ooe with == ? thanks!
 
 Benchmark it both ways and find out.  Then post back and tell us.  :)

Started to, but found I cant ;( 

I need to get the key from the array also (for use in a corresponding
array) and with in_array() I couldnt find a way to do it. So, im just
using == which is lightning fast anyway so no worries :)

thanks again..

-- 
Nick W

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php