How best to check if a user is from China and hide content?

To speed up my website in China I thought of hiding blocked services from Chinese users and wrote this and put it in functions.php

$isInChina = false; 
$ip = $_SERVER['REMOTE_ADDR']; // This will contain the ip of the request 
// This service tells me where the IP address is from and gives me more data than I need. 
$userData = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip)); 
if (is_Null($userData) || empty($userData) || $userData-geoplugin_countryCode == "CN")
{ 
    $isInChina = true; // Count no data as in China maybe to be paranoid. 
}

Shouldn't I store the result in some session level variable?

Topic functions php Wordpress

Category Web


I'd recommend using something like this https://www.geoplugin.com/ and write an if statement for a either a display none or noscript. If you're looking for a good code base to start with I'd take a look at this answer. https://stackoverflow.com/a/13600004/3175165

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.