Prohibit specified IP addresses from accessing the website

Source: Internet
Author: User
Provides various official and user-released code examples. For code reference, you are welcome to learn how to add a forbidden IP library. By modifying the top-level class, you can disable the IP addresses in the library from accessing the website, it is commonly known as an IP address.
After analyzing the website Error Log recently, we found that a large number of frequently-occurring IP addresses are continuously scanning the website, such:
[2014-04-28T21: 55: 26 + 08: 00] 223.208.36.53/dede/
ERR: Unable to load Controller: Dede
[2014-04-28T09: 01: 24 + 08: 00] 115.29.196.101/wwwroot.rar
ERR: Unable to load Controller: Wwwroot.rar
[2014-04-28T09: 01: 25 + 08: 00] 115.29.196.101/wwwroot.zip
ERR: Unable to load Controller: Wwwroot.zip
[2014-04-28T09: 01: 25 + 08: 00] 115.29.196.101/wwwroot.7z
ERR: Unable to load Controller: Wwwroot.7z
[2014-04-28T09: 01: 25 + 08: 00] 115.29.196.101/bak.zip
ERR: Unable to load Controller: Bak.zip
[2014-04-28T09: 01: 25 + 08: 00] 115.29.196.101/bak.rar
ERR: Unable to load Controller: Bak.rar
[2014-04-28T09: 01: 25 + 08: 00] 115.29.196.101/bak.7z
ERR: Unable to load Controller: Bak.7z

These records clearly indicate that someone is using a program to search for the website's background addresses or backup files in batches. It looks uncomfortable, although the program apparently rejects the request.

As a result, the function of adding IP addresses to prohibit all these IP addresses from accessing the website.

I thought I could start with the top-level class, so I found the top-level Controller class of the framework and added the following code in the _ construct () method: $IP = get_client_ip();
$iplist = M('iplist')->field('ip')->select();
for($i=0;$i $list[$i] = long2ip($iplist[$i]['ip']);
}
if(in_array($IP,$list)){
exit('You don\'t have permission to access!');
}
This Code reads the list of restricted IP addresses from the database and uses the visitor IP address to determine whether the data table exists. If yes, an error is returned. Otherwise, no operation is performed.
Of course, adding such code to a top-level class may affect the performance. It doesn't matter. Add the _ construct () method directly to the controllers of each group, the same is true for writing the above Code. However, this is a little more troublesome, and the number of controllers should be written.
Note that the __construct () method cannot be used in disorder. If it is written in the controller, parent ::__ construct () must be added at the beginning; otherwise, an error is reported!

Other tasks:
In the background development function, administrators can add or delete prohibited IP addresses. Note that the IP address must be extracted from the error log to identify illegal access. Do not disable the search engine IP address!

AD: truly free, domain name + VM + enterprise mailbox = 0 RMB

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.