最近不知道做了什么修改,在后台评论区查看访客ip时居然全部显示成本站主机所在的ip地址,原因不详
所以很难排查,只能在wp-config.php里面加入如下代码,让wp显示正确的访客ip。
/** 正确记录访客ip. */ if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']); $_SERVER['REMOTE_ADDR'] = $list[0]; }
还有一种直接通过第三方API获取
采用curl模拟打开搜狐ip页面的方式正则匹配获得ip
$url = "http://txt.go.sohu.com/ip/soip"; $ch = curl_init(); curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT,10); $pageContent = curl_exec($ch); $getip = array(); preg_match_all("/p="(.*)";s/isU",$pageContent, $getip); $ip1 = (curl_getinfo($ch, CURLINFO_HTTP_CODE)==200 && isset($getip[1][0])) ? $getip[1][0] : $_SERVER['REMOTE_ADDR']; $_SERVER['REMOTE_ADDR'] = $ip1;
添加之后IP就会正确显示了。