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]
<?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;
}
}
?>
[/php]

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

[divider]Notes[/divider]

1. What does CloudFlare IP Geolocation do?

2 Comments

Add a Comment

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