If your site is behind CloudFlare, you cannot access your visitors IP / Country by using :
1 |
echo $_SERVER['REMOTE_ADDR']; |
Because it gives you a CloudFlare IP…
Very frustrating if you are trying to block some countries,
Here is the TIP :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?php // You can add this code to your header // display real visitor's IP // echo "CloudFlare Ip ".$_SERVER["HTTP_CF_CONNECTING_IP"]."<br>"; // display real visitor's country code // echo "CloudFlare Country ".$_SERVER["HTTP_CF_IPCOUNTRY"]."<br>"; $country=$_SERVER["HTTP_CF_IPCOUNTRY"]; // Test if visitor is from some countries and redirect them if so if (($country=='TH') OR ($country=='AF') OR ($country=='PK') OR ($country=='IN') OR ($country=='MM')) { // Display a nice message ;-) // echo "pay klai klai !"; // or redirect this visitor to a nice page ?> <script type="text/javascript"> document.location="https://web-a-way.com"; </script> <?php } ?> |
That’s it !
Take a look at https://underthebo.com to see it live,
To discover the full website, you can buy a plane ticket or use a VPN…