Php ip address-Based Access Control Code

Source: Internet
Author: User

Assume that the IP address range is 192.168.0.0 ~ The code that only allows access to this IP segment is as follows:
Copy codeThe Code is as follows:
<? Php
$ 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:
Copy codeThe Code is as follows:
<? Php
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:
Copy codeThe Code is as follows:
<? Php
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.