douxian9706 2017-11-05 14:19
浏览 184
已采纳

如何在PHP中阻止所有CloudFlare IP

I have a problem with some proxy sites that are using cloudflare and they are cloaking and spamdexing my website.

How can i block all cloudflare ip's in php so these sites that are scraping my website get blocked .. or is there any better solution? My website is also using cloudflare.

  • 写回答

2条回答 默认 最新

  • doujia1939 2017-11-05 23:43
    关注

    luckily, cloudflare provides a list of their IP ranges here, so just check if the connecting IP is within 1 of those ranges, and exit() if it is.

    example implementation using the M6Web/Firewall:

    use M6Web\Component\Firewall\Firewall;
    
    if(!((new Firewall())->setDefaultState(true)->addList(file('blacklist.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES),'localBad')->setIpAddress($_SERVER['REMOTE_ADDR'])->handle())){
         http_response_code(403);
         exit();
    }
    

    with an accompanying daily cronjob:

    <?php
    $ips = file_get_contents ( 'https://www.cloudflare.com/ips-v4' ) . "
    " . file_get_contents ( 'https://www.cloudflare.com/ips-v6' );
    file_put_contents ( '/path/to/blacklist.txt', $ips );
    
    • note that it would be difficult to implement ipv6 cidr ranges manually, thus you should probably use a 3rd-party libary, like the M6Web firewall. also, it would be much more performant to do it with iptables than at the php level.

    • the cronjob is not really required, you can fetch a fresh list of ips with every pageload, but that would probably be very slow, and, perhaps ironically, you'd might get auto ip-banned from cloudflare.com for spamming, thus i highly suggest you use a daily cronjob.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题