summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRick Ellis <rick.ellis@ellislab.com>2007-06-11 07:03:11 +0200
committerRick Ellis <rick.ellis@ellislab.com>2007-06-11 07:03:11 +0200
commite666afc7eaf8c8224c4e6cf6f7e415966f22783e (patch)
tree3e14ef859a90216a4fb8c0f2f6e99fea1f9c4b3d
parent8d127c78fe0a1d6034cb503529dc548b566156c8 (diff)
-rw-r--r--system/libraries/Input.php36
1 files changed, 17 insertions, 19 deletions
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;
}
}