NYCPHP Meetup

NYPHP.org

[nycphp-talk] Best practice for comparing IP addresses

Chris Snyder chris at psydeshow.org
Sun Jun 1 10:50:47 EDT 2003


Phil Powell wrote:

>What I want to do is compare an IP address from a parsed XML file with the user's IP address.  If a match is found only in the first three numbers (xx.xx.xx.*) then action is taken.  I thought about using the following algorithm to do so, but does anyone else have a better suggestion?  I am not accustomed to either implode, explode or array_pop, so I am not sure if they are being used properly w/o extensive testing.
>
>  
>
I think you've already found a pretty elegant solution. The only other 
way I can think of to do it would be:

$lastdot= strrpos($ipaddress, ".");
$network= substr($ipaddress, 0, $lastdot);

Or even $network= substr( $ipaddress, 0, strrpos ($ipaddress, ".") ); 
which would displense with the arrays.

    chris.





More information about the talk mailing list