0°

禁止国外外网IP访问WordPress,防止扫描及攻击

1352 个字符,0 张图片,大约需要 2 分钟阅读
/**
* WordPress 显示评论者IP归属地PHP函数
* https://www.aliuyun.cn/wordpress/wordpress-course/management/jinzhi-waiguo-ip.html
**/
function aliuyun_getRealIp()
{
  $ip=false;
  if(!empty($_SERVER["HTTP_CLIENT_IP"])){
    $ip = $_SERVER["HTTP_CLIENT_IP"];
  }
  if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
    if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
    for ($i = 0; $i < count($ips); $i++) {
      if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
        $ip = $ips[$i];
        break;
      }
    }
  }
  return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}

function aliuyun_locate($ip) {
    $ip=aliuyun_getRealIp();
    if(empty($ip)) $ip = get_comment_author_IP();
    $ch = curl_init();  
    $timeout = 5;  
    curl_setopt ($ch, CURLOPT_URL, 'http://ip.taobao.com/service/getIpInfo.php?ip='.$ip);  
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
    $file_contents = curl_exec($ch);  
    curl_close($ch);  
    $result = json_decode($file_contents,true);
    if ($result['data']['country'] != '中国') {
        wp_die('FUCK YOU!!!!!!!!!!', 'FUCK YOU!', array('response' => '503'));
    } else {
        echo '';
    }
}
add_action( 'wp_footer', 'aliuyun_locate' );

来源:阿柳云

除非注明,否则均为喵喵喵博客原创文章,转载请以链接形式标明本文地址

本文链接:https://www.miaomiaomiao.org/26.html

0 条回复 A 作者 M 管理员
    当上帝赐给你荒野时,就意味着,他要你成为高飞的鹰
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论