PHP如何阻止我的站点中的代理? [英] PHP How to Block Proxies from my Site?

查看:53
本文介绍了PHP如何阻止我的站点中的代理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找阻止代理进入我的网站的绝对最佳方法.原因是由于我在项目中使用了唯一的IP地址.

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.

您会推荐什么?

谢谢!

推荐答案

我不知道实现此目的的防弹方法,但这几乎是完整的:

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';
}

此get_ip_address()函数已被修改

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'];
}

这篇关于PHP如何阻止我的站点中的代理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆