2

WordPress: CloudFlare-Country-Login

This snippet allows us to tap into the CF_IPCountry [1] header and determine if this country is allowed to login. We put allowed countries into the array situated in the cf_country_login() function.
Put this snippet into your theme’s functions.php and you’re all done, but don’t forget to first edit $urel and the default array of allowed countries.

<?php
// Allow only logins from specified countries (requires CloudFlare IP Geolocation to be enabled)
add_action( 'wp_authenticate', 'cf_country_login', 1);
function cf_country_login()
{
  if ( !isset($_SERVER["HTTP_CF_IPCOUNTRY"]) )
  	return;

	if ( !in_array($_SERVER["HTTP_CF_IPCOUNTRY"], array('DK')) )
	{
                $urel = 'http://k0nsl.org';
                echo 'Sorry, but you cannot login at this time.';
                header("Refresh: 5; url='.$urlel.'");
		exit;
	}
}
?>

Available also @ GitHub:
https://github.com/k0nsl/k0nsl-misc/blob/master/CloudFlare-Country-Login

Notes

1. What does CloudFlare IP Geolocation do?

k0nsl

k0nsl

k0nsl is a programmer, server administrator, designer, moderator, spokesman and factotum of a wide-variety of web pages. You can reach k0nsl via e-mail — i.am@k0nsl.org.

2 Comments

  1. Thank you for the plugin. This blog in general has very informative and useful content, I like it very much.
    I am bookmarking your page, so I can visit it again. And I hope I will get something new from your page next time.

    Take care!

    Regards,
    Jenny.

  2. Hi..how do you add more than one country to the array? i tried adding the codes with commas between, but it doesnt work.

    thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *