Re: how to check REMOTE_ADDR to see if it contains PART of an ip

From: Date: Wed, 16 Jan 2002 20:22:52 +0000
Subject: Re: how to check REMOTE_ADDR to see if it contains PART of an ip
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-80813@lists.php.net to get a copy of this message
At 10:58 AM 1/16/2002 -0800, Police Trainee wrote:
in a nutshell: if ("150.200.250.x" is within $REMOTE_ADDR){dowhatever}
There are a couple of different ways to accomplish this. One is: if (strpos($REMOTE_ADDR,"150.200.250.") !== false) { //do whatever } You have to be careful with strpos()...note the !== operator which is required because strpos() can return an integer 0 meaning "I found your string at position 0", or it can return a boolean FALSE indicating "I didn't find your string at all". If you don't use the extra = then you won't know the difference between those two cases, since an integer 0 is implicitly converted to boolean FALSE if you aren't also checking for variable type. This will especially cause a problem in your case, because the string you are searching for will ALWAYS be found at position 0 if it's found at all. BTW, I include the trailing period so that you don't accidentally match an ip such as "208.150.200.250" The second way is to use a regular expression: if (ereg("^150\.200\.250",$REMOTE_ADDR)) { //do whatever } The trailing period isn't necessary here because I'm using the ^ symbol which anchors the match to the beginning of the string. To me the second example is more intuitive but is probably less efficient in terms of execution time...

« previous php.general (#80813) next »