note 86587 modified in function.substr by danbrown

From: Date: Fri, 24 Oct 2008 21:40:37 +0000
Subject: note 86587 modified in function.substr by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-145725@lists.php.net to get a copy of this message
Here we have gr8 function which simply convert ip address to a number using substr with negative offset. You can need it if you want to compare some IP addresses converted to a numbers. For example when using ip2country, or eliminating same range of ip addresses from your website :D <?php function ip2no($val) { list($A,$B,$C,$D) = explode(".",$val); return substr("000".$A,-3). substr("000".$B,-3). substr("000".$C,-3). substr("000".$D,-3); } $min = ip2no("10.11.1.0"); $max = ip2no("111.11.1.0"); $visitor = ip2no("105.1.20.200"); if($min<$visitor && $visitor<$max) { echo 'Welcome !'; } else { echo 'Get out of here !'; } ?> --was-- Here we have gr8 function which simply convert ip address to a number using substr with negative offset. You can need it if you want to compare some IP addresses converted to a numbers. For example when using ip2country, or eliminating same rang of ip addresses from your website :D <? function ip2no($val) { list($A,$B,$C,$D) = explode(".",$val); return substr("000".$A,-3). substr("000".$B,-3). substr("000".$C,-3). substr("000".$D,-3); } $min = ip2no("10.11.1.0"); $max = ip2no("111.11.1.0"); $visitor = ip2no("105.1.20.200"); if($min<$visitor && $visitor<$max) { echo 'Welcome !'; } else { echo 'Get out of here !'; } ?> http://php.net/manual/en/function.substr.php

« previous php.notes (#145725) next »