今天就是讲给REMOTE_ADDR不可以伪造的,就在curl 中也无法伪造 相对是比较安全的服务端ip获取方法,当然,也有可能被路由伪造 这个不好说,因为REMOTE_ADDR 是底层的回话ip地址,路由是可以发起伪造。所以,网上很多人都在问这个问题,也有很多人不死心,但现实确实是残酷的 也是完美的
给个演示案例你吧:你就信了
1.将以下代码保存为 Client.php
<?php //php脚本开始<?php $ch = curl_init();$url = " $header = array( 'CLIENT-IP:208.165.188.175', 'X-FORWARDED-FOR:208.165.188.175, ); //声明伪造head请求头 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); $page_content = curl_exec($ch); curl_close($ch); echo $page_content; ?>
2.将以下文件保存为ser.php
<?php //php脚本开始<?php echo getenv('HTTP_CLIENT_IP'); echo getenv('HTTP_X_FORWARDED_FOR'); echo getenv('REMOTE_ADDR'); ?>
3.运行结果
//html脚本开始 208.165.188.175 208.165.188.175 127.0.0.1 //上面结果可看出,http_client_ip,http_x_forwarded_for 都被伪造了而remote_addr 还是127.0.0.1 就是客户端ip
4.附上一个PHP获取IP地址函数
function get_real_ip($type = 0,$adv=false) { $type = $type ? 1 : 0; static $ip = NULL; if ($ip !== NULL) return $ip[$type]; if($adv){ if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown',$arr); if(false !== $pos) unset($arr[$pos]); $ip = trim($arr[0]); }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; }elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } }elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } // IP地址合法验证 $long = sprintf("%u",ip2long($ip)); $ip = $long ? array($ip, $long) : array('0.0.0.0', 0); return $ip[$type];}
评论回复