Welcome Guest, Not a member yet? Register   Sign In
Supporting IP6 when fetching and storing IP addresses
#7

[eluser]bretticus[/eluser]
Funny I got bit by this today.

I use a mac to develop with (I'm somewhat new to using a mac too.) For some reason, my local address is ::1 instead of 127.0.0.1. I have a little debug helper in a certain project that checks to see if my ip is a debugger ip or not. well, I stuck in ::1 to my list, but the valid_ip method was returning false and ultimately 0.0.0.0 for me. So I rewrote valid_ip().

NOTE: This is not a fix for CI. It relies upon the filter_var() function which is PHP5 => 5.2.0. Also, if filter_var is not available, it goes back to ipv4 checkign only. I needed this to be quick (deadlines!)

However, I thought I'd pass it along as it may help you out (like me.)

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Input extends CI_Input
{
    
    /** Updated version suggested by Geert De Deckere (modified by Brett Millett)
    *
    * @access    public
    * @param    string
    * @return    string
    */

    function valid_ip($ip)
    {
            if ( function_exists('filter_var') ) { //PHP 5 >= 5.2.0 only.
                //check ipv4
                if( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== FALSE ) {
                    return TRUE;
                } elseif ( filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE ) {
                    return TRUE;
                } else {
                    return FALSE;
                }
            } else {
                // ipv4 only. todo implement ipv6 pattern matching
                $ip_segments = explode('.', $ip);

                // Always 4 segments needed
                if (count($ip_segments) != 4)
                {
                    return FALSE;
                }
                // IP can not start with 0
                if ($ip_segments[0][0] == '0')
                {
                    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 ($segment == '' OR preg_match("/[^0-9]/", $segment) OR $segment > 255 OR strlen($segment) > 3)
                    {
                        return FALSE;
                    }
                }
            }

            return TRUE;
    }

}
?>


Messages In This Thread
Supporting IP6 when fetching and storing IP addresses - by El Forum - 09-07-2009, 01:04 PM
Supporting IP6 when fetching and storing IP addresses - by El Forum - 09-07-2009, 03:59 PM
Supporting IP6 when fetching and storing IP addresses - by El Forum - 09-07-2009, 04:03 PM
Supporting IP6 when fetching and storing IP addresses - by El Forum - 09-07-2009, 04:08 PM
Supporting IP6 when fetching and storing IP addresses - by El Forum - 09-07-2009, 04:11 PM
Supporting IP6 when fetching and storing IP addresses - by El Forum - 09-08-2009, 10:28 AM
Supporting IP6 when fetching and storing IP addresses - by El Forum - 09-08-2009, 03:45 PM
Supporting IP6 when fetching and storing IP addresses - by El Forum - 09-08-2009, 07:45 PM
Supporting IP6 when fetching and storing IP addresses - by El Forum - 09-08-2009, 08:06 PM



Theme © iAndrew 2016 - Forum software by © MyBB