PHP code _ php instance for IP address-based access control

Source: Internet
Author: User
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 ');
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.