Sometimes you need to control access based on IP addresses to restrict or guide certain access requests. For example, allow normal access from visitors in the LAN segment, but prohibit access from the Internet. Mango briefly introduces how to use PHP scripts to implement this function. Assume that the IP address range is 192.168.0.0 ~ The code that only allows access to this IP segment is as follows:
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 ";
?>
In the process, this code first captures the visitor's IP address and then determines whether the IP address meets the access conditions. If yes, the page is output normally. otherwise, access is denied.
Therefore, if the user's IP address meets the requirements, you only need to simply output or include a page file. The file contains the following 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 based on the judgment results. The jump code is as follows:
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 ');
?>