简体   繁体   English

PHP:如何检查脚本调用程序的IP地址

[英]PHP: how to check ip address of the script invoker

I need to add an if statement on top of my php script in order to check the ip of the script invoker: 我需要在我的php脚本顶部添加if语句,以便检查脚本调用者的ip:

if (invoker_ip = "123.123.123.123") {
    .. code here ..
}

the script is just a script.php file on my server 该脚本只是我服务器上的script.php文件

thanks 谢谢

$ip = '123.123.132.132';

if ($_SERVER['REMOTE_ADDR'] === $ip) {
    // This will only excexuted if the IP matches
    echo 'IP does match :)';
} else {
    echo 'IP does not match!';
}
invoker_ip = $_SERVER['REMOTE_ADDR']

询问$ _SERVER ['REMOTE_ADDR'],但要小心代理。

Use : 采用 :

$invoker_ip = $_SERVER['REMOTE_ADDR'];

Nevertheless this value will not be relevant if user is behind a proxy. 但是,如果用户位于代理之后,则此值将不相关。 The real ip probably can be found in HTTP header like HTTP_CLIENT_IP or HTTP_X_FORWARDED_FOR but can't be trusted. 真正的ip可能在HTTP标头中找到,例如HTTP_CLIENT_IP或HTTP_X_FORWARDED_FOR,但不能被信任。 (header are easy to modify) (标题易于修改)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM