How to disable access from an ip address or ip address segment in php _ PHP Tutorial

Source: Internet
Author: User
Php disables access from an ip address or ip address segment. This article describes how php disables access from an ip address or ip address segment, operating skills involving SERVER variables $ _ SERVER, files, and strings php methods for disabling access from an ip address or ip address segment

This article mainly introduces how php disables access from an ip address or ip address segment, and involves the operating skills of the SERVER variable $ _ SERVER, files, and strings. it has some reference value, for more information, see

This article describes how php disables access from an ip address or ip address segment and shares it with you for your reference. The specific analysis is as follows:

Because I have not studied Apache and nginx in depth, I used PHP to write a small program that can disable the address.
You only need:

?

1

You can.

Disable a single ip address as follows:

?

1

2

3

4

5

6

7

8

9

10

// Disable the IP address

$ Ip = $ _ SERVER ["REMOTE_ADDR"];

$ Ban = file_get_contents ("ban. dat ");

If (stripos ($ ban, $ ip ))

{

Die ("Your IP Address is: $ ip, you're forbiden to view this page! ");

}

Echo "Your IP Address is: $ ip, hello! ";

?>

The ban. dat file is as follows:

?

1

2

3

4

BEGIN:

119.184.251.245

127.0.0.1

192.168.1.100

The disabled ip segment is as follows:

?

1

2

3

4

5

6

7

8

9

10

11

// Disable the IP address

$ Ip = $ _ SERVER ["REMOTE_ADDR"];

While ($ ip [count ($ IP-1)]! = '.') $ Ip = substr ($ ip, 1,-1); // sort out the ip segment

$ Ban = file_get_contents ("ban. dat ");

If (stripos ($ ban, $ ip ))

{

Die ("U'RE forbiden to view this page! ");

}

Echo "Hello! ";

?>

I hope this article will help you with php programming.

This article describes how php disables access from an ip address or ip address segment. it involves the operating skills of the SERVER variable $ _ SERVER, file, and string...

Related Article

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.