Giter VIP home page Giter VIP logo

cf-ip-rewrite's Introduction

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

$is_cf = CloudFlare\IpRewrite::isCloudFlare();
$original_ip = CloudFlare\IpRewrite::getOriginalIP();
$rewritten_ip = CloudFlare\IpRewrite::getRewrittenIP();

The class exposes three methods for interaction. A call to any of these methods will trigger the rewriting, storing the rewritten values for any subsequent calls. If the IP is rewritten, $_SERVER["REMOTE_ADDR"] will also be updated to reflect the end-user's IP address.

CloudFlare\IpRewrite::isCloudFlare(); returns true if the CF_CONNECTING_IP header is present in the request. If the rewrite happened at the server level, this function will still return true, even though it did not rewrite the IP address.

CloudFlare\IpRewrite::getOriginalIP() returns the original ip address from $_SERVER["REMOTE_ADDR"], before this module alters it.

CloudFlare\IpRewrite::getRewrittenIP() returns the rewritten ip address, if a rewrite occurs, otherwise it will return false.

Best practice

The best course of action is to call CloudFlare\IpRewrite::isCloudFlare(); to guarantee that a rewrite has occured. From that point forward, $_SERVER["REMOTE_ADDR"] can be used to retrieve the end-users IP address (whether the call came through CloudFlare or not).

CloudFlare\IpRewrite::getOriginalIP() and CloudFlare\IpRewrite::getRewrittenIP() should be used if you need visibility into the rewrite that has occured.

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

phpunit -c phpunit.xml.dist

With code coverage report in coverage folder

phpunit -c phpunit.xml.dist --coverage-html coverage

cf-ip-rewrite's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.