如何最好地检查用户是否来自中国并隐藏内容?

时间:2019-01-09 作者:Muskie

为了加快我在中国的网站速度,我想对中国用户隐藏被屏蔽的服务,于是写了这篇文章并将其放入函数中。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. 
}
我不应该将结果存储在某个会话级别变量中吗?

1 个回复
SO网友:Dustin Snider

我建议使用this https://www.geoplugin.com/ 并为display none或noscript编写if语句。如果你想从一个好的代码库开始,我会看看this answer. https://stackoverflow.com/a/13600004/3175165