Rumah > pembangunan bahagian belakang > tutorial php > php禁止ip访问的函数

php禁止ip访问的函数

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Lepaskan: 2016-07-25 09:00:26
asal
1473 orang telah melayarinya
为大家提供一个可以禁止IP访问的函数,包含一个信任IP访问的函数,有需要的朋友可以参考下。

完整代码如下。

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

66

67

68

69

70

71

72

73

74

75

<?

/**

* 禁止IP访问的函数

* 搜集 程序员之家 bbs.it-home.org

*/

function check_ip($range,$ip='') {

if($ip == '') $ip = getenv("REMOTE_ADDR");

if ($ip == "127.0.0.1") return 1;

$result = 1;

if (ereg("([0-9]+).([0-9]+).([0-9]+).([0-9]+)/([0-9]+)",$range,$regs)) {

   $ipl = ip2long($ip);

   $rangel = ip2long($regs[1] . "." . $regs[2] . "." . $regs[3] . "." . $regs[4]);

 

   $maskl = 0;

 

for ($i = 0; $i< 31; $i++) {

if ($i < $regs[5]-1) {

$maskl = $maskl + pow(2,(30-$i));

}

}

 

if (($maskl & $rangel) == ($maskl & $ipl)) {

   return 1;

} else {

   return 0;

}

} else {

 

$maskocts = split(".",$range);

   $ipocts = split(".",$ip);

   for ($i=0; $i<4; $i++) {

     if (ereg("[([0-9]+)-([0-9]+)]",$maskocts[$i],$regs)) {

      if ( ($ipocts[$i] > $regs[2]) || ($ipocts[$i] < $regs[1])) {

         $result = 0;

      }

}

else

{

if ($maskocts[$i] <> $ipocts[$i]) {

    $result = 0;

}

}

}

}

return $result;

}

 

function auth_ip(){

   $result = 0;

   $null_check = 1;

 

   $path = "ip.cf"; //ip限制配置文件,每行的格式为

/*

xxx.xxx.xxx.xxx 比如 127.0.0.2

xxx.xxx.xxx.[yyy-zzz] 比如 1270.0.0.[2-23]

xxx.xxx.xxx.xxx/nn 比如 127.0.0.0/24

*/

$fg=@fopen($path,"r");

 

while($line=@fgets($fg,1024)){

$line = trim($line);

$line=ereg_replace("#.*","",$line);

if ($line != ""){

$null_check = 0;

if (check_ip($line)) {

fclose($fg);

return 1;

}

}

}

@fclose($fg);

if ($null_check == 1) return 1;

return $result;

}

?>

Salin selepas log masuk
您可能感兴趣的文章: PHP禁止IP访问网站 php 禁止单个IP地址或IP段访问


Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan