From e666afc7eaf8c8224c4e6cf6f7e415966f22783e Mon Sep 17 00:00:00 2001 From: Rick Ellis Date: Mon, 11 Jun 2007 05:03:11 +0000 Subject: --- system/libraries/Input.php | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'system/libraries/Input.php') diff --git a/system/libraries/Input.php b/system/libraries/Input.php index f113cff71..e493f6d33 100644 --- a/system/libraries/Input.php +++ b/system/libraries/Input.php @@ -379,36 +379,34 @@ class CI_Input { /** * Validate IP Address * + * Updated version suggested by Geert De Deckere + * * @access public * @param string * @return string */ function valid_ip($ip) { - if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $ip)) + $ip_segments = explode('.', $ip); + + // Always 4 segments needed + if (count($ip_segments) != 4) { return FALSE; } - - $octets = explode('.', $ip); - - for ($i = 1; $i <= 4; $i++) + // IP cannot start with 0 + if (substr($ip_segments[0], 0, 1) == 0) { - $octet = intval($octets[($i-1)]); - if ($i === 1) - { - if ($octet > 223 OR $octet < 1) - return FALSE; - } - elseif ($i === 4) - { - if ($octet < 1) - return FALSE; - } - else + return FALSE; + } + // Check each segment + foreach ($ip_segments as $segment) + { + // IP segments must be digits and can not be + // longer than 3 digits or greater then 255 + if ( ! ctype_digit($segment) OR $segment > 255 OR strlen($segment) > 3) { - if ($octet > 254) - return FALSE; + return FALSE; } } -- cgit v1.2.3-24-g4f1b