前言
获取评论用户IP属地依赖wordpress保存的ip地址,然后利用接口获取该用户所在地区!非常简单
效果图
操作方法
将下方代码添加到functions.php或者你创建的func.php文件里(/zibll/functions.php)都是可以的
//获取地址
function xypro_user_ip($user_ip){
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => "https://v1.21lhz.cn/API/IP/api.php?domian=".$user_ip,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/x-www-form-urlencoded",
),
));
$url = curl_exec($ch);
$a = json_decode($url,true);
curl_close($ch);
$dizhi = $a['country'];
$dizhi = mb_substr($dizhi, 0, 3, 'utf-8');
return $dizhi;
}
//获取结束
这里需要到zib-comments-list.php修改(在zibll/inc/functions),先搜索$replyText_html跳到第二个,然后粘贴下方的代码覆盖它即可!
$user_ip = $comment->comment_author_IP;
$replyText_html = '<span class="reply-link">' . $replyText . '</span><span style="cursor:pointer" data-toggle="tooltip" alt="IP:'.$user_ip.'" data-original-title="IP地址:'.xypro_user_ip($user_ip).'">'.xypro_user_ip($user_ip).'</span>';
网站挂载了cdn,还需要获取用户的真实IP地址需要添加一行代码,才可以的呦!
WordPress根目录找到这个 wp-config.php 在文件添加以下代码即可:
//获取用户真实ip地址
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$list = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);
$_SERVER['REMOTE_ADDR'] = $list[0];
}
感谢您的来访,获取更多精彩文章请收藏本站。
Invitation
wuyunyun
-Scan
created:2022/05/10
Nice to meet you!
Use this card to join the wuyunyun's Space and participate in a pleasant discussion together .
Welcome to wuyunyun's Space,wish you a nice day .
© 版权声明
THE END
暂无评论内容