Limit IP access to ideas and PHP code

xiaoxiao2021-03-06  40

Write your own website that day, think of having to restrict IP's function, at least to include IP to be limited in the background, and can limit the IP segment, such as 192.168.0. * The same can limit the entire section. I think about a few IF judgments, and use the simple code as follows: (using the DB class in phplib)

Error_Reporting (7); session_start ();

// Send a 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");

/ ***************** Can clients can access this website to check *********** /

// Get Client IPif (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'];}

// Decomposing client IP $ CIP = EXPLODE (".", $ Client_ip);

// Connect the database $ db = new db_sql (); $ ERR = $ db-> connect ();

/ * Limit remote IP access, PS: This code is really dizzy, huh, take 8 IF, -_- # * /// From the database to extract stored IP addresses $ query_str = "SELECT LIMIT_IP from US_Limitip "; $ dB-> query ($ query_str); // extract the results, one by one, check while ($ db-> next_record ()) {$ limited_ip = $ db-> f (" limit_ip "); $ LIP = EXPLODE (".", $ limit_ip); // If the first IP is first * or 0, jump to the error page IF (($ lip [0] == '*') || ($ LIP [0] == '0')) Header ("location: ./ error.php? errid = 300"); // If the client IP is equal to our restriction IP, jump to the error page IF ($ client_ip = = $ limit_ip) Header ("location: ../ error.php? errid = 300"); // If the first group IP matches the second group of IP matches IF ($ CIP [0] == $ LIP [0 ]) {// If the second set limit IP is * jump to the error page IF ($ location: ./ error.php? Errid = 300 "); / / Second group IP matching, the third group IP matching IF ($ CIP [1] == $ LIP [1]) {// If the third set limit character is * jump to the error page IF ($ LIP [2 ] == '*') Header ("location: ../ error.php? Errid = 300"); // If the third group IP matches jump to the third set of check IF ($ CIP [2] == $ LIP [2]) {// If the fourth group restriction IP is * or 0, jump to the error page IF (($ lip [3] == '*') || ($ lip [3] == '0 ')) HEADER ("location: ../ error.php? Errid = 300");}}}} // Release database query results $ db-> free (); / ********** ******** IP check end ***************** /

?> The code is just an idea I initially, definitely there is not enough, if the master has better suggestions, please don't know!

转载请注明原文地址:https://www.9cbs.com/read-69566.html

New Post(0)