How does PHP obtain only ipv4 addresses?

Source: Internet
Author: User
You want to display different content on the website based on different IP address segments. Currently, ipv4 address segments are restricted. However, some clients access ipv4 addresses, and the limitations are useless. So I would like to ask whether it is possible to obtain only ipv4 code through php. The current ph... is used to display different content on the website based on different IP address segments. Currently, ipv4 address segments are restricted. However, some clients access ipv4 addresses, and the limitations are useless. So I would like to ask whether it is possible to obtain only ipv4 code through php.
The php code used to obtain the ip address is as follows:

function check_ip($str)    {        return preg_match('/""A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))"".){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))""Z/',$str)>0;    }    function getIP($isHeader=false)    {        if($isHeader)        {            if (isset($_SERVER['HTTP_CLIENT_IP']) && check_ip($_SERVER['HTTP_CLIENT_IP']))            {                return $_SERVER['HTTP_CLIENT_IP'];            }            else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && check_ip($_SERVER['HTTP_X_FORWARDED_FOR']))            {                return $_SERVER['HTTP_X_FORWARDED_FOR'];            }        }        if (isset($_SERVER['REMOTE_ADDR']) && check_ip($_SERVER['REMOTE_ADDR']))        {            return $_SERVER['REMOTE_ADDR'];        }        else        {            return 'unknown';        }    }

Reply content:

You want to display different content on the website based on different IP address segments. Currently, ipv4 address segments are restricted. However, some clients access ipv4 addresses, and the limitations are useless. So I would like to ask whether it is possible to obtain only ipv4 code through php.
The php code used to obtain the ip address is as follows:

function check_ip($str)    {        return preg_match('/""A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))"".){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))""Z/',$str)>0;    }    function getIP($isHeader=false)    {        if($isHeader)        {            if (isset($_SERVER['HTTP_CLIENT_IP']) && check_ip($_SERVER['HTTP_CLIENT_IP']))            {                return $_SERVER['HTTP_CLIENT_IP'];            }            else if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && check_ip($_SERVER['HTTP_X_FORWARDED_FOR']))            {                return $_SERVER['HTTP_X_FORWARDED_FOR'];            }        }        if (isset($_SERVER['REMOTE_ADDR']) && check_ip($_SERVER['REMOTE_ADDR']))        {            return $_SERVER['REMOTE_ADDR'];        }        else        {            return 'unknown';        }    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.