{"id":1014,"date":"2024-05-25T09:53:38","date_gmt":"2024-05-25T09:53:38","guid":{"rendered":"https:\/\/www.appfinz.com\/blogs\/?p=1014"},"modified":"2025-05-15T05:03:26","modified_gmt":"2025-05-15T05:03:26","slug":"cloudflare-and-logging-visitor-ip-addresses-via-in-php","status":"publish","type":"post","link":"https:\/\/www.appfinz.com\/blogs\/cloudflare-and-logging-visitor-ip-addresses-via-in-php\/","title":{"rendered":"CloudFlare and logging visitor IP addresses via in PHP"},"content":{"rendered":"\n<p>CloudFlare has released a module&nbsp;<code>mod_cloudflare<\/code>&nbsp;for apache, the module will log and display the actual visitor IP Addresses rather than those accessed by cloudflare!&nbsp;<a href=\"https:\/\/www.cloudflare.com\/resources-downloads#mod_cloudflare\">https:\/\/www.cloudflare.com\/resources-downloads#mod_cloudflare<\/a>&nbsp;(Answer by:&nbsp;<a href=\"https:\/\/www.cloudflare.com\/ips\/\">olimortimer<\/a>)<\/p>\n\n\n\n<p><strong><em>If you dont have access to the apache runtime you can use the script below, this will allow you to check if the connection was through cloudflare and get the users ip.<\/em><\/strong><\/p>\n\n\n\n<p>I am rewriting my answer i used for another question &#8220;<a href=\"https:\/\/stackoverflow.com\/a\/38298000\/2801045\">CloudFlare DNS \/ direct IP identifier<\/a>&#8220;<\/p>\n\n\n\n<p>Cloudflare&#8217;s ips are stored in public so you can go view them&nbsp;<a href=\"https:\/\/www.cloudflare.com\/ips\/\">here<\/a>&nbsp;then check if the ip is from cloudflare (this will allow us to get the real ip from the http header&nbsp;<code>HTTP_CF_CONNECTING_IP<\/code>).<\/p>\n\n\n\n<p>If you are using this to disable all non cf connections or vice versa, i recommend you to have a single php script file that gets called before every other script such as a common.php or pagestart.php etc.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">function ip_in_range($ip, $range) {\n    if (strpos($range, '\/') == false)\n        $range .= '\/32';\n\n    \/\/ $range is in IP\/CIDR format eg 127.0.0.1\/24\n    list($range, $netmask) = explode('\/', $range, 2);\n    $range_decimal = ip2long($range);\n    $ip_decimal = ip2long($ip);\n    $wildcard_decimal = pow(2, (32 - $netmask)) - 1;\n    $netmask_decimal = ~ $wildcard_decimal;\n    return (($ip_decimal &amp; $netmask_decimal) == ($range_decimal &amp; $netmask_decimal));\n}\n\nfunction _cloudflare_CheckIP($ip) {\n    $cf_ips = array(\n        '199.27.128.0\/21',\n        '173.245.48.0\/20',\n        '103.21.244.0\/22',\n        '103.22.200.0\/22',\n        '103.31.4.0\/22',\n        '141.101.64.0\/18',\n        '108.162.192.0\/18',\n        '190.93.240.0\/20',\n        '188.114.96.0\/20',\n        '197.234.240.0\/22',\n        '198.41.128.0\/17',\n        '162.158.0.0\/15',\n        '104.16.0.0\/12',\n    );\n    $is_cf_ip = false;\n    foreach ($cf_ips as $cf_ip) {\n        if (ip_in_range($ip, $cf_ip)) {\n            $is_cf_ip = true;\n            break;\n        }\n    } return $is_cf_ip;\n}\n\nfunction _cloudflare_Requests_Check() {\n    $flag = true;\n\n    if(!isset($_SERVER['HTTP_CF_CONNECTING_IP']))   $flag = false;\n    if(!isset($_SERVER['HTTP_CF_IPCOUNTRY']))       $flag = false;\n    if(!isset($_SERVER['HTTP_CF_RAY']))             $flag = false;\n    if(!isset($_SERVER['HTTP_CF_VISITOR']))         $flag = false;\n    return $flag;\n}\n\nfunction isCloudflare() {\n    $ipCheck        = _cloudflare_CheckIP($_SERVER['REMOTE_ADDR']);\n    $requestCheck   = _cloudflare_Requests_Check();\n    return ($ipCheck &amp;&amp; $requestCheck);\n}\n\n\/\/ Use when handling ip's\nfunction getRequestIP() {\n    $check = isCloudflare();\n\n    if($check) {\n        return $_SERVER['HTTP_CF_CONNECTING_IP'];\n    } else {\n        return $_SERVER['REMOTE_ADDR'];\n    }\n}<\/pre><\/div>\n\n\n\n<p>To use the script it&#8217;s quite simple:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;php&quot;,&quot;mime&quot;:&quot;text\/x-php&quot;,&quot;theme&quot;:&quot;midnight&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:true,&quot;readOnly&quot;:false,&quot;fileName&quot;:&quot;PHP&quot;,&quot;language&quot;:&quot;PHP&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;php&quot;}\">$ip = getRequestIP();\n$cf = isCloudflare();\n\nif($cf) echo &quot;Connection is through cloudflare: &lt;br&gt;&quot;;\nelse    echo &quot;Connection is not through cloudflare: &lt;br&gt;&quot;;\n\necho &quot;Your actual ip address is: &quot;. $ip;\necho &quot;The server remote address header is: &quot;. $_SERVER['REMOTE_ADDR'];<\/pre><\/div>\n\n\n\n<p>This script will show you the real ip address and if the request is CF or not!<\/p>\n\n\n\n<p class=\"has-background\" style=\"background-color:#eff6fe\">If you\u2019re planning to revamp your online presence, don\u2019t miss exploring our\u00a0<a href=\"https:\/\/www.appfinz.com\/website-designing-company-in-delhi\"><strong>website designing services<\/strong><\/a>\u00a0and custom\u00a0<strong><a href=\"https:\/\/www.appfinz.com\/website-development-company-in-delhi\">website development<\/a><\/strong>\u00a0solutions tailored to your business needs.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>CloudFlare has released a module&nbsp;mod_cloudflare&nbsp;for apache, the module will log and display the actual visitor<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1014","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/comments?post=1014"}],"version-history":[{"count":2,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1014\/revisions"}],"predecessor-version":[{"id":1370,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/posts\/1014\/revisions\/1370"}],"wp:attachment":[{"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/media?parent=1014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/categories?post=1014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appfinz.com\/blogs\/wp-json\/wp\/v2\/tags?post=1014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}