Home php教程 php手册 php判断ip黑名单程序代码

php判断ip黑名单程序代码

May 25, 2016 pm 04:45 PM
code program blacklist

学校的新闻系统要求有些新闻只开放校内ip浏览,于是重写了一个代码来实现此功能,实现后的结果是,只要把允许访问的ip列入ip.txt这个文件中即可,同时支持c类ip,例如:

1

2

3

4

ip.txt

192.168

211.67.188

211.67.191.25

Copy after login

PHP实例代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<?php

/*

 * ip地址黑名单、白名单

 * 判断访客地址的ip是否在ip.txt中,支持c类ip

 * By xhat

*/

$ip = $_SERVER[&#39;REMOTE_ADDR&#39;];

$ipArray = preg_replace("#rn?|n#", "", file(&#39;ip.txt&#39;));

foreach ($ipArray as $ipTest) {

    if (substr_count($ip, $ipTest) != "0") {

        echo "ok"; //执行相关命令

        die();

    }

}

?>

Copy after login

上面大家可以使用代码来骗过了,下面代码进行升级:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

<?php

class block_ip {

    var $Block_ip = array(

        "192.168.1.1",

        "210.10.2.1-20",

        "222.34.4.*"

    );

    function __construct() {

    }

    function __destruct() {

    }

    private function makePregIP($str) {

        if (strstr($str, "-")) {

            $aIP = explode(".", $str);

            foreach ($aIP as $k => $v) {

                if (!strstr($v, "-")) {

                    $preg_limit.= makePregIP($v);

                } else {

                    $aipNum = explode("-", $v);

                    for ($i = $aipNum[0]; $i <= $aipNum[1]; $i++) {

                        $preg.= $preg ? "|" . $i : "[" . $i;

                    }

                    $preg_limit.= strrpos($preg_limit, ".", 1) == (strlen($preg_limit) - 1) ? $preg . "]" : "." . $preg . "]";

                }

            }

        } else {

            $preg_limit.= $str . ".";

        }

        return $preg_limit;

    }

    private function getAllBlockIP() {

        if ($this->Block_ip) {

            foreach ($this->Block_ip as $k => $v) {

                $ipaddres = $this->makePregIP($v->start_ip);

                $ip = str_ireplace(".", ".", $ipaddres);

                $ip = str_replace("*", "[0-9]{1,3}", $ip);

                $ipaddres = "/" . $ip . "/";

                $ip_list[] = $ipaddres;

            }

        }

        return $ip_list;

    }

    public function checkIP() {

        $iptable = $this->getAllBlockIP();

        $IsJoined = true;

        //取得用户ip

        $Ip = $this->get_client_ip();

        $Ip = trim($Ip);

        //剔除黑名单中的IP区段

        if ($iptable) {

            foreach ($iptable as $value) {

                if (preg_match("{$value}", $Ip)) {

                    $IsJoined = false;

                    break;

                }

            }

        }

        // 如果在ip黑名单中就执行如下操作

        if (!$IsJoined) {

            echo "IP Error";

            exit;

        }

    }

    private function get_client_ip() {

        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP") , "unknown")) $ip =

Copy after login

getenv("HTTP_CLIENT_IP");

else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR") , "unknown")) $ip = getenv("HTTP_X_FORWARDED_FOR");

else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR") , "unknown")) $ip = getenv("REMOTE_ADDR");

else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) $ip = $_SERVER['REMOTE_ADDR'];

else $ip = "unknown";

return ($ip);

}

}

?>

引用片段,代码如下:

1

2

$oBlock_ip = new block_ip();

$oBlock_ip->checkIP();

Copy after login


教程链接:

随意转载~但请保留教程地址★

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to make Google Maps the default map in iPhone How to make Google Maps the default map in iPhone Apr 17, 2024 pm 07:34 PM

The default map on the iPhone is Maps, Apple's proprietary geolocation provider. Although the map is getting better, it doesn't work well outside the United States. It has nothing to offer compared to Google Maps. In this article, we discuss the feasible steps to use Google Maps to become the default map on your iPhone. How to Make Google Maps the Default Map in iPhone Setting Google Maps as the default map app on your phone is easier than you think. Follow the steps below – Prerequisite steps – You must have Gmail installed on your phone. Step 1 – Open the AppStore. Step 2 – Search for “Gmail”. Step 3 – Click next to Gmail app

How to solve win7 driver code 28 How to solve win7 driver code 28 Dec 30, 2023 pm 11:55 PM

Some users encountered errors when installing the device, prompting error code 28. In fact, this is mainly due to the driver. We only need to solve the problem of win7 driver code 28. Let’s take a look at what should be done. Do it. What to do with win7 driver code 28: First, we need to click on the start menu in the lower left corner of the screen. Then, find and click the "Control Panel" option in the pop-up menu. This option is usually located at or near the bottom of the menu. After clicking, the system will automatically open the control panel interface. In the control panel, we can perform various system settings and management operations. This is the first step in the nostalgia cleaning level, I hope it helps. Then we need to proceed and enter the system and

How to restore friends from WeChat blacklist? Where can I find people who have been blocked by WeChat? How to restore friends from WeChat blacklist? Where can I find people who have been blocked by WeChat? Feb 22, 2024 pm 05:58 PM

Just turn off the blacklist switch on the other party's homepage. Analysis 1 First enter the WeChat My page and click Settings. 2After entering the settings page, click Privacy. 3. Enter the privacy page and click on the address book blacklist. 4 Enter the address book blacklist and click on the friend who needs to be restored. 5. Go to your friend's homepage and click on the three-dot icon in the upper right corner. 6. Enter the data settings page and turn off the switch to add to the blacklist. Supplement: Does the other party know about the blocked WeChat friend? 1 When a friend is blocked on WeChat, the other party will not receive any prompts, and your WeChat account will still be displayed in the other party's address book friend list, but when the other party contacts you, it will You receive a message that the message you sent was rejected, and the other party cannot view your Moments and updated avatar. Summary/Notes on Micro

What to do if the blue screen code 0x0000001 occurs What to do if the blue screen code 0x0000001 occurs Feb 23, 2024 am 08:09 AM

What to do with blue screen code 0x0000001? The blue screen error is a warning mechanism when there is a problem with the computer system or hardware. Code 0x0000001 usually indicates a hardware or driver failure. When users suddenly encounter a blue screen error while using their computer, they may feel panicked and at a loss. Fortunately, most blue screen errors can be troubleshooted and dealt with with a few simple steps. This article will introduce readers to some methods to solve the blue screen error code 0x0000001. First, when encountering a blue screen error, we can try to restart

Steps to set up a blacklist on an Apple phone Steps to set up a blacklist on an Apple phone Mar 26, 2024 pm 07:00 PM

1. Click the dial button. 2. Click on the address book and select the blocked contact. 3. Click to block this caller number.

Clock app missing in iPhone: How to fix it Clock app missing in iPhone: How to fix it May 03, 2024 pm 09:19 PM

Is the clock app missing from your phone? The date and time will still appear on your iPhone's status bar. However, without the Clock app, you won’t be able to use world clock, stopwatch, alarm clock, and many other features. Therefore, fixing missing clock app should be at the top of your to-do list. These solutions can help you resolve this issue. Fix 1 – Place the Clock App If you mistakenly removed the Clock app from your home screen, you can put the Clock app back in its place. Step 1 – Unlock your iPhone and start swiping to the left until you reach the App Library page. Step 2 – Next, search for “clock” in the search box. Step 3 – When you see “Clock” below in the search results, press and hold it and

How to completely delete WeChat blacklist How to completely delete WeChat blacklist Apr 18, 2024 pm 01:46 PM

1. Open the WeChat app, click [Me] in the lower right corner, then click [Settings] - [Friend Permissions] - [Contact Book Blacklist]. 2. Find the person you want to delete completely in the blacklist and click to enter their personal homepage. 3. Click the three dots in the upper right corner of the profile page and select [Delete] in the pop-up menu.

Can't allow access to camera and microphone in iPhone Can't allow access to camera and microphone in iPhone Apr 23, 2024 am 11:13 AM

Are you getting "Unable to allow access to camera and microphone" when trying to use the app? Typically, you grant camera and microphone permissions to specific people on a need-to-provide basis. However, if you deny permission, the camera and microphone will not work and will display this error message instead. Solving this problem is very basic and you can do it in a minute or two. Fix 1 – Provide Camera, Microphone Permissions You can provide the necessary camera and microphone permissions directly in settings. Step 1 – Go to the Settings tab. Step 2 – Open the Privacy & Security panel. Step 3 – Turn on the “Camera” permission there. Step 4 – Inside, you will find a list of apps that have requested permission for your phone’s camera. Step 5 – Open the “Camera” of the specified app

See all articles