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

PHP code to control access based on IP address_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:38:45
Original
1263 people have browsed it

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,'...
Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template