[PHP] how to check REMOTE_ADDR to see if it contains PART of an ip
hello. i am trying to write a simple php line that checks the REMOTE_ADDR variable and see if it comes from a specific netblock (to see if A.B.C is contained in a REMOTE_ADDR of A.B.C.D) But how do i do this? i know there is a php function that checks the string for the presence of another one... any help please? the php site is only helpful to me if i know which function i wish to look up by name. in a nutshell: if (150.200.250.x is within $REMOTE_ADDR){dowhatever} thanks! __ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] how to check REMOTE_ADDR to see if it contains PART of an ip
nah, there's another one that you don't have to speficy the length. i remember seeing it used once when checking the user's browser to see if the string mozilla was contained within the variable... besides, i changed the string to check for by one digit and it still returned true because PART of the string was true i guess... i dunno. anyone else got another function in mind? --- [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: strncmp() MANUAL! bvr. On Wed, 16 Jan 2002 10:41:51 -0800 (PST), Police Trainee wrote: hello. i am trying to write a simple php line that checks the REMOTE_ADDR variable and see if it comes from a specific netblock (to see if A.B.C is contained in a REMOTE_ADDR of A.B.C.D) But how do i do this? i know there is a php function that checks the string for the presence of another one... any help please? the php site is only helpful to me if i know which function i wish to look up by name. in a nutshell: if (150.200.250.x is within $REMOTE_ADDR){dowhatever} thanks! __ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] __ Do You Yahoo!? Send FREE video emails in Yahoo! Mail! http://promo.yahoo.com/videomail/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] how to check REMOTE_ADDR to see if it contains PART of an ip
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... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] how to check REMOTE_ADDR to see if it contains PART of an ip
yeah, like strpos() you still could have gotten from the manual. strncmp() does it exactly, like strpos() but only searches the beginning, note that it returns false for matching strings for use in some sorting functions. ?php $testip = 150.200.250.x; $match = 150.200.250.; if (!strncmp($testip, $match, strlen($match))) { echo(ok, . $testip . matches); } ? bvr. On Wed, 16 Jan 2002 10:58:16 -0800 (PST), Police Trainee wrote: nah, there's another one that you don't have to speficy the length. i remember seeing it used once when checking the user's browser to see if the string mozilla was contained within the variable... besides, i changed the string to check for by one digit and it still returned true because PART of the string was true i guess... i dunno. anyone else got another function in mind? --- [EMAIL PROTECTED] [EMAIL PROTECTED] wrote: strncmp() MANUAL! bvr. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]