0

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?

4
  • It's not clear what you're actually trying to block / where you want to use this information. Can you elaborate on that (in the question itself, preferrably in relations to WordPress).
    – kero
    Commented Jan 9, 2019 at 12:05
  • If you haven't heard everything is blocked in China including DISQUS, Google, Facebook, Pinterest, Instagram etc. So if you have say the Twitter widget in your sidebar it won't work and it will slow your page load speed down drastically. So you have two options rip out all social media integration or hide it from users in China. I have tried both, the above code works, but it seems wasteful to call the geoplugin.net URL every time, shouldn't I cache the result in the session somehow?
    – Muskie
    Commented Jan 9, 2019 at 12:36
  • 1
    I know this is not the comment you are hoping for, but the best method would be to make a second page for the chinese market. Not only, as you said, they have different restrictions, but also they have a extremely different marketing language than western countries.
    – marvinpoo
    Commented Jan 9, 2019 at 12:51
  • The third option I have is remove all social media integration from the theme which is what I have running right now. It is fast and honestly my website is not popular. I will explore other options such as those listed below or that I have turned up by searching.
    – Muskie
    Commented Jan 11, 2019 at 6:12

2 Answers 2

0

Yes, storing the result in a session-level variable is a good idea for performance optimization. By doing so, you avoid making repeated requests to the external geolocation service every time a page is loaded, which can slow down your website and increase the load on that service.

Here’s how you can modify your code to store the result in a session variable:

session_start(); // Start the session at the beginning of your script

if (!isset($_SESSION['isInChina'])) {
    $isInChina = false; 
    $ip = $_SERVER['REMOTE_ADDR']; // Get the user's IP address
    $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; // If no data or if the country code is China, assume user is in China
    }
    
    $_SESSION['isInChina'] = $isInChina; // Store the result in session
} else {
    $isInChina = $_SESSION['isInChina']; // Retrieve the result from session
}

Benefits:

  • Performance: Reduces the number of external requests by storing the result in the session, which speeds up subsequent page loads.

  • Efficiency: Reduces the load on the geolocation service by avoiding unnecessary calls.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.