admin管理员组文章数量:1122832
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(".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?
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?
Share Improve this question edited Jan 9, 2019 at 12:03 kero 6,3101 gold badge25 silver badges34 bronze badges asked Jan 9, 2019 at 11:18 MuskieMuskie 1151 silver badge8 bronze badges 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
Reset to default 0Yes, 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.
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
本文标签: phpHow best to check if a user is from China and hide content
版权声明:本文标题:php - How best to check if a user is from China and hide content? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736296755a1929867.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论