承接 cloudflare/cf-ip-rewrite 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

cloudflare/cf-ip-rewrite

最新稳定版本:1.0.4

Composer 安装命令:

composer require cloudflare/cf-ip-rewrite

包简介

Library to rewrite CloudFlare IP Addresses to the end-user IP address

README 文档

README

This module makes it easy for developers to add rewrite Cloudflare IP Addresses for actual end-user IP Addresses at the application layer. It is recommended to either install mod_cloudflare for Apache or use nginx rewrite rules (https://support.cloudflare.com/hc/en-us/articles/200170706-Does-CloudFlare-have-an-IP-module-for-Nginx-) if possible.

For those cases, where the IP can not be guaranteed to be rewritten by one of these alternate means, this module can be used to rewrite the IP address.

How it works

    $ipRewrite = new CloudFlare\IpRewrite();
    $is_cf = $ipRewrite->isCloudFlare();
    $rewritten_ip = $ipRewrite->getRewrittenIP();
    $original_ip = $ipRewrite->getOriginalIP();

The class exposes three methods for interaction and a constructor.

Initializing IpRewrite() object will try to rewrite the IP. If the IP is rewritten, $_SERVER["REMOTE_ADDR"] will be updated to reflect the end-user's IP address.

isCloudFlare(); returns true if the CF_CONNECTING_IP header is present in the request and the request originates from a Cloudflare IP.

getRewrittenIP() Returns the rewritten ip address if a rewrite occurs, otherwise it will return null.

getOriginalIP() returns the saved original ip address from $_SERVER["REMOTE_ADDR"].

Best Pratice

    // Initialize object to rewrite the headers
    try {
        $ipRewrite = new CloudFlare\IpRewrite();
    } catch (RuntimeException $e) {
        // PHP configurations does not support IPv6
    }
    
    // Check if the request is from Cloudflare
    $is_cf = $ipRewrite->isCloudFlare();
    if ($is_cf) {
        // Get original or rewritten ip
        // Order does not matter
        ...
        $rewritten_ip = $ipRewrite->getRewrittenIP();
        ...
        $original_ip = $ipRewrite->getOriginalIP();
        ...
    }

Caution

Rewrite action is triggered only once in constructor. If getRewrittenIP() or getOriginalIP() is called multiple times it'll return the first result regardless if a change happened after the first call. Since rewrite action was not triggered.

To get the newest changes a new IpRewrite object should be used.

Testing this module

This module comes with a set of tests that can be run using phpunit. To run the tests, run composer install on the package and then one of the following commands:

Basic Tests

composer test

With code coverage report in coverage folder

vendor/bin/phpunit -c phpunit.xml.dist --coverage-html coverage

统计信息

  • 总下载量: 669k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 14
  • 点击次数: 2
  • 依赖项目数: 4
  • 推荐数: 2

GitHub 信息

  • Stars: 14
  • Watchers: 23
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固