Idea of restricting IP Access and PHP code

Source: Internet
Author: User

I wrote my own website that day and thought about the IP address restriction function. At least I should be able to set the IP address to be restricted in the background, and I should be able to restrict the IP address segment, such as 192.168.0. * The entire segment can be restricted in the same way. The simple code is as follows: (the DB class in phplib is used in the Program)

<? PHP
/*************************************** ******
* File: limitip. php
* Purpose: IP address restriction Program
* Version: V1.0
* Date:
* Author: heiyeluren (heiyeluren@163.com)
* Copyright: http://www.unixsky.net
**************************************** *****/

Error_reporting (7 );
Session_start ();

// Send character header information
If ($ headercharset)
Header ("Content-Type: text/html; charset = gb2312 ");

// Load public files
Require_once ("config. php ");
Require_once ("Global. php ");
Require_once ("db_mysql.php ");

/***************** Check whether the client can access the website ************/

// Obtain the Client IP Address
If (getenv ('HTTP _ client_ip ')){
$ Client_ip = getenv ('HTTP _ client_ip ');
} Elseif (getenv ('HTTP _ x_forwarded_for ')){
$ Client_ip = getenv ('HTTP _ x_forwarded_for ');
} Elseif (getenv ('remote _ ADDR ')){
$ Client_ip = getenv ('remote _ ADDR ');
} Else {
$ Client_ip = $ http_server_vars ['remote _ ADDR '];
}

// Break down the Client IP Address
$ CIP = explode (".", $ client_ip );

// Connect to the database
$ Db = new db_ SQL ();
$ Err = $ db-> connect ();

/* Restrict access from remote IP addresses. PS: This code is dizzy. It uses 8 If ,-_-#*/
// Extract the restricted IP address from the database
$ Query_str = "select limit_ip from us_limitip ";
$ Db-> query ($ query_str );
// Cyclically extract the results for verification.
While ($ db-> next_record ())
{
$ Limit_ip = $ db-> F ("limit_ip ");
$ Lip = explode (".", $ limit_ip );
// If the first IP address to be restricted is * or 0, it will jump to the error page.
If ($ lip [0] = '*') | ($ lip [0] = '0 '))
Header ("Location:.../error. php? Errid = 300 ");
// If the client IP address is equal to the IP address limit, it will jump to the error page.
If ($ client_ip = $ limit_ip)
Header ("Location:.../error. php? Errid = 300 ");
// If the first group of IP addresses are consistent, match the second group of IP addresses
If ($ CIP [0] = $ lip [0])
{
// If the second group limits IP addresses *, the page jumps to the error page.
If ($ lip [1] = '*')
Header ("Location:.../error. php? Errid = 300 ");
// Match the second group of IP addresses.
If ($ CIP [1] = $ lip [1])
{
// Jump to the error page if the third character group is *
If ($ lip [2] = '*')
Header ("Location:.../error. php? Errid = 300 ");
// If the third group of IP addresses matches, the request is forwarded to the third group for verification.
If ($ CIP [2] = $ lip [2])
{
// If the fourth group of restricted IP addresses is * or 0, the page will jump to the error page.
If ($ lip [3] = '*') | ($ lip [3] = '0 '))
Header ("Location:.../error. php? Errid = 300 ");
}
}
}
}
// Release the database query results
$ Db-> free ();

******************/

?>

The code is just one of my initial ideas, and there must be some shortcomings. If you have better suggestions, please don't hesitate to give me some advice!

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.