PHP code example of cloudflare / cf-ip-rewrite

1. Go to this page and download the library: Download cloudflare/cf-ip-rewrite library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cloudflare / cf-ip-rewrite example snippets


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

    // 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();
        ...
    }