INET_ATON() and INET_NTOA() in PHP?

后端 未结 6 2247
予麋鹿
予麋鹿 2020-12-02 18:59

I want to store IP addresses in my database, but I also need to use them throughout my application. I read about using INET_ATON() and INET_NTOA()

6条回答
  •  广开言路
    2020-12-02 19:28

    Here PHP alternative functions (simple copy/paste in your program) -

    function inet_aton($ip)
    {
        $ip = trim($ip);
        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) return 0;
        return sprintf("%u", ip2long($ip));  
    }
    
    
    function inet_ntoa($num)
    {
        $num = trim($num);
        if ($num == "0") return "0.0.0.0";
        return long2ip(-(4294967295 - ($num - 1))); 
    }
    

提交回复
热议问题