PHPackages                             cloudflare/cf-ip-rewrite - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. cloudflare/cf-ip-rewrite

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cloudflare/cf-ip-rewrite
========================

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

1.0.4(8y ago)15752.7k↓19.9%11[3 PRs](https://github.com/cloudflare/cf-ip-rewrite/pulls)4MITPHPPHP &gt;=5.3.0CI failing

Since Mar 6Pushed 1y ago22 watchersCompare

[ Source](https://github.com/cloudflare/cf-ip-rewrite)[ Packagist](https://packagist.org/packages/cloudflare/cf-ip-rewrite)[ Docs](https://github.com/cloudflare/cf-ip-rewrite)[ RSS](/packages/cloudflare-cf-ip-rewrite/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (2)Versions (8)Used By (4)

Cloudflare PHP IP Rewriting
===========================

[](#cloudflare-php-ip-rewriting)

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 () 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

[](#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

[](#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

[](#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

[](#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

[](#basic-tests)

```
composer test

```

#### With code coverage report in `coverage` folder

[](#with-code-coverage-report-in-coverage-folder)

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

```

###  Health Score

46

—

FairBetter than 93% of packages

Maintenance29

Infrequent updates — may be unmaintained

Popularity49

Moderate usage in the ecosystem

Community31

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 67.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~189 days

Recently: every ~113 days

Total

6

Last Release

3142d ago

Major Versions

v0.5.0 → 1.0.02016-07-12

### Community

Maintainers

![](https://www.gravatar.com/avatar/c79189ba99de1d27fea43e4ad20d52415c60f8f037a2c0f2be9629bbc9bde9cc?d=identicon)[nkcmr](/maintainers/nkcmr)

![](https://www.gravatar.com/avatar/873dd23d6e74daeea30addd3381d0031af80f5545a4f7f99d1a0628ab475d090?d=identicon)[manatarms](/maintainers/manatarms)

![](https://www.gravatar.com/avatar/2926c91753a8f8899d56ac51aa0208446b8d15f33e3ca717dff853c3f909902f?d=identicon)[jacobbednarz](/maintainers/jacobbednarz)

![](https://www.gravatar.com/avatar/259666b32454ecdff28b3ebd432510348e6237041732cda1f26272c0e3c0e35a?d=identicon)[patryk](/maintainers/patryk)

---

Top Contributors

[![thellimist](https://avatars.githubusercontent.com/u/6690196?v=4)](https://github.com/thellimist "thellimist (21 commits)")[![jwineman](https://avatars.githubusercontent.com/u/6947158?v=4)](https://github.com/jwineman "jwineman (4 commits)")[![hrushikeshdeshpande](https://avatars.githubusercontent.com/u/161167942?v=4)](https://github.com/hrushikeshdeshpande "hrushikeshdeshpande (3 commits)")[![IcyApril](https://avatars.githubusercontent.com/u/2026099?v=4)](https://github.com/IcyApril "IcyApril (1 commits)")[![tholu](https://avatars.githubusercontent.com/u/316763?v=4)](https://github.com/tholu "tholu (1 commits)")[![XhmikosR](https://avatars.githubusercontent.com/u/349621?v=4)](https://github.com/XhmikosR "XhmikosR (1 commits)")

---

Tags

cloudflareip rewrite

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/cloudflare-cf-ip-rewrite/health.svg)

```
[![Health](https://phpackages.com/badges/cloudflare-cf-ip-rewrite/health.svg)](https://phpackages.com/packages/cloudflare-cf-ip-rewrite)
```

###  Alternatives

[monicahq/laravel-cloudflare

Add Cloudflare ip addresses to trusted proxies for Laravel.

3372.7M4](/packages/monicahq-laravel-cloudflare)[orrison/cumulus

Import DNS records from Laravel Vapor into Cloudflare

3817.1k](/packages/orrison-cumulus)[molayli/laravel-cloudflare-real-ip

Get the real ip for laravel applications behind cloudflare's reverse proxy

1221.0k](/packages/molayli-laravel-cloudflare-real-ip)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
