Current location - Quotes Website - Personality signature - How to implement IP restrictions using php
How to implement IP restrictions using php

lt;?php

error_reporting(7);

session_start();

//Send character header information

if ($headercharset)

header("Content-Type: text/html; charset=gb2312");

//Load public *** file

require_once("config.php");

require_once("global.php");

require_once("db_mysql.php");

/**************** Verify whether the client can access this website************/

//Get the client IP

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'];

}

//Decompose the client IP

$cip = explode(".", $client_ip);

//Connect to the database

$db = new DB_Sql();

$err = $db-gt; connect();

/* Restrict remote IP access, PS: This code is really confusing, haha, use it 8 ifs, -_-# */

// Extract the stored IP address to be restricted from the database

$query_str = "SELECT limit_ip FROM us_limitip";

p>

$db-gt; query($query_str);

// Extract the results in a loop and verify them one by one

while ($db-gt; next_record ())

{

$limit_ip = $db-gt;f("limit_ip");

$lip = explode(".", $ limit_ip);

//If the first character in the limit IP 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 happens to be equal to our limit IP, it will jump to the error page

if ($client_ip==$limit_ip)

header("Location: ../error.php?errid =300");

//

If the first set of IPs are consistent, match the second set of IPs

if ($cip[0] == $lip[0])

{

// If the second group of restricted IP is *, jump to the error page

if ($lip[1]=='*')

header("Location: ../ error.php?errid=300");

//The second set of IP matches will be matched with the third set of IPs

if ($cip[1]==$lip[ 1])

{

// If the third set of restricted characters is *, jump to the error page

if ($lip[2]==' *')

header("Location:../error.php?errid=300");

// If the third group of IPs matches, jump to the third group of schools. Verify

if ($cip[2]==$lip[2])

{

// If the fourth group of restricted IP is * or 0 Just jump to the error page

if (($lip[3]=='*') || ($lip[3]=='0'))

header( "Location: ../error.php?errid=300");

}

}

}

}

// Release database query results

$db-gt; free();

/****************** ***IP verification ended******************/gt;