Use Taobao’s IP interface to determine whether the IP is a domestic IP. If it is domestic (CN), access is not allowed.
$ip = $_SERVER['REMOTE_ADDR']; $content = file_get_contents(‘http://ip.taobao.com/service/getIpInfo.php?ip='.$ip); $banned = json_decode(trim($content), true); $lan = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']); if((!empty($banned['data']['country_id']) && $banned['data']['country_id'] == ‘CN') || strstr($lan, ‘zh')) { header(“HTTP/1.0 404 Not Found”); echo ‘HTTP/1.0 404 Not Found'; exit; }
I also found a good article: http://luhuang.sinaapp.com/redis-setnx/ "Redis to limit high-concurrency php code examples"
Redis is essentially a key-value database, but while maintaining the simplicity and speed of key-value databases, it also absorbs some of the advantages of relational databases. Thus its position is between relational database and key-value database. Redis can not only save Strings type data, but also Lists type (ordered) and Sets type (unordered) data, and can also complete advanced functions such as sorting (SORT). When implementing INCR, SETNX and other functions, It ensures the atomicity of its operations. In addition, it also supports master-slave replication and other functions.
Redis to limit high concurrency
php code example
$redis->setnx(‘lock:hot_items', true)尝试创建一个key作为”锁”.若key已存在,setnx不会做任何动作且返回值为false,所以只有一个客户端会返回true值进入if语句更新缓存. $redis = new redis(); $redis_key = ‘lock:hot_items'; $clock_expire_time = $redis->get($redis_key); if(!empty($clock_expire_time) && time() > intval($clock_expire_time)) { //解除当前Redis锁 $redis->delete($redis_key); } if($redis->setnx($redis_key, time() + 3) !== true) { echo ‘高并发有冲突'; } //操作你的代码, 同一时刻就一个人访问该代码了 //解除当前Redis锁 $redis->delete($redis_key);