Use PHP code to restrict domestic IP access to our website _php tips

Source: Internet
Author: User

The use of Taobao IP interface to determine whether IP, is the domestic IP, is the domestic (CN) is not allowed access.

$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, ' en '))
{
header ("http/1.0 404 Not Found");
echo ' http/1.0 404 Not Found ';
Exit;
}

Also found a good article: http://luhuang.sinaapp.com/redis-setnx/"Redis to limit high concurrent PHP code example"

Redis is essentially a key-value database, but it also absorbs the advantages of some relational databases while maintaining the simple and quick features of key-valued databases. So that it is positioned between the relational database and the key value database. Redis not only can save strings type of data, but also can save lists type (ordered) and sets type (unordered) data, but also can complete sorting (sort) and other advanced functions, in the implementation of functions such as incr,setnx to ensure the atomic nature of its operations, in addition, also supports the master-slave replication and other functions.
Redis to limit high concurrency
PHP Code Instance

 $redis->setnx (' Lock:hot_items ', true) tries to create a key as a "lock." If key already exists, 
SETNX does not do any action and the return value is false, so only one client returns a true value into the IF statement to update the cache.
$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)) {//unlock current Redis lock $redis->delete ($
Redis_key);

} if ($redis->setnx ($redis _key, Time () + 3)!== true) {echo ' High concurrency conflict ';} 
Manipulate your code, at the same time a person accesses the code//unlocks the current Redis lock $redis->delete ($redis _key); 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.