PHP How to Block Proxies from my Site?

后端 未结 7 1620
迷失自我
迷失自我 2021-01-03 11:39

I\'m looking for the absolute best way of blocking proxies coming onto my site. The reason is due to me using unique IP address\'s on my project.

What would you reco

7条回答
  •  [愿得一人]
    2021-01-03 12:33

    I'm not aware of a bulletproof way to do this, but this would be pretty much complete:

    if (get_ip_address() !== get_ip_address(true))
    {
        echo 'using proxy';
    }
    

    This get_ip_address() function was adapted from this answer and goes as follows:

    function get_ip_address($proxy = false)
    {
        if ($proxy === true)
        {
            foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED') as $key)
            {
                if (array_key_exists($key, $_SERVER) === true)
                {
                    foreach (array_map('trim', explode(',', $_SERVER[$key])) as $ip)
                    {
                        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== false)
                        {
                            return $ip;
                        }
                    }
                }
            }
        }
    
        return $_SERVER['REMOTE_ADDR'];
    }
    

提交回复
热议问题