PHP implementation of filter IP black and white list method

Source: Internet
Author: User
    1. function Ip_test ($ip, $iprule) {
    2. $ipruleregexp =str_replace ('. * ', ' ph ', $iprule);
    3. $ipruleregexp =preg_quote ($ipruleregexp, '/');
    4. $ipruleregexp =str_replace (' ph ', ' \.[ 0-9]{1,3} ', $ipruleregexp);
    5. if (Preg_match ('/^ '. $ipruleregexp. ' $/', $ip)) return true;
    6. else return false;
    7. }
Copy Code

After implementing the core function of ip_test, the following procedure is much simpler, simply traversing each rule in the list, judging whether the IP currently connected is compliant with the rules, and doing the corresponding steps. Whitelist to continue the operation when the IP satisfies at least one rule:

    1. $curr _ip=$_server[' remote_addr '];
    2. $white _list=array (...); White List Rules
    3. $test _success=false;
    4. foreach ($white _list as $iprule) {
    5. if (Ip_test ($curr _ip, $iprule)) {
    6. $test _success=true;
    7. Break
    8. }
    9. }
    10. if (! $test _success) exit (' IP not at White List ');
Copy Code

Blacklist, continue operation when IP does not satisfy all rules:

    1. $curr _ip=$_server[' remote_addr '];
    2. $black _list=array (...); Blacklist rules
    3. foreach ($black _list as $iprule) {
    4. if (Ip_test ($curr _ip, $iprule)) {
    5. Exit (' IP in black List ');
    6. }
    7. }
Copy Code

At this point, a simple PHP implementation of the IP black-and-white list filter is complete, hoping to bring you help. Articles you may be interested in:

PHP Security Filter Code (360 provides high safety factor) PHP filter Post,get sensitive data Instance code PHP filter illegal with special string method PHP anti-injection a section of code (filter parameters) Very good PHP code to prevent SQL Injection Vulnerability Filter function PHP prevent SQL injection Regular filtering a case of PHP filtering dangerous HTML code

  • 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.