Home  >  Article  >  Backend Development  >  PHP code to control access based on IP address_PHP tutorial

PHP code to control access based on IP address_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:38:451189browse

Assume that there is an IP address range of 192.168.0.0~192.168.0.255. The code that only allows access to this IP segment is as follows:

Copy the code The code is as follows:

$IP = $_SERVER['REMOTE_ADDR'];
$from = strcmp($IP,'192.168.0.0');
$to = strcmp($ IP,'192.168.0.255');
if (!($from >= 0 && $to <= 0))
echo "Access Denied";
else
echo "Homepage ";
?>

Process-wise, this code first captures the visitor's IP address, and then determines whether the IP address meets the access conditions. If it matches, the page will be output normally, otherwise access will be denied.

According to this, if the user IP address meets the requirements, simply output or include the page file. The file contains the following code:
Copy code The code is as follows:

if (!($from >= 0 && $to <= 0))
echo "Access Denied";
else
include('homepage.html')";
?>

Of course, you can also jump to different pages according to the judgment results. The jump code is as follows:
Copy the code The code is as follows:

if (!($from >= 0 && $to <= 0))
header('Location: http://www.jb51.net/404. html');
else
header('Location: http://www.jb51.net/index.html');
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321726.htmlTechArticleAssume there is an IP address range 192.168.0.0~192.168.0.255. The code that only allows access to this IP segment is as follows: Copy The code is as follows: ?php $IP = $_SERVER['REMOTE_ADDR']; $from = strcmp($IP,'...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn