Home  >  Article  >  Backend Development  >  How to limit the IP address range in php_PHP tutorial

How to limit the IP address range in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:58:33894browse

How to limit the IP address range in PHP

Only IP addresses within the limited range can be accessed

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

function get_real_ipaddress() {

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {

return $_SERVER['HTTP_CLIENT_IP'];

} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

return $_SERVER['HTTP_X_FORWARDED_FOR'];

}

return $_SERVER['REMOTE_ADDR'];

}

function in_ip_range($ip, $ip_one, $ip_two = false) {

if(!$ip_two) {

return $ip_one === $ip;

}

return ip2long($ip_one) * -1 >= ip2long($ip) * -1 && ip2long($ip_two) * -1 <= ip2long($ip) * -1;

}

function validate_ip() {

$ip = explode(':', get_real_ipaddress());

$ip = $ip[0];

if(in_ip_range($ip, '212.76.229.115', '212.76.229.120')) {

return true;

} else if(in_ip_range($ip, '194.78.4.66', '194.78.4.79')) {

return true;

} else if(in_ip_range($ip, '194.8.4.78', '194.8.4.78')) {

return true;

} else if(in_ip_range($ip, '0', '1')) { // local

return true;

}

header('Location: http://www.bkjia.net');

}

1

2

3

4

5

6

8 9 10 11 12
13

14
15<๐ŸŽœ> <๐ŸŽœ>16<๐ŸŽœ> <๐ŸŽœ>17<๐ŸŽœ> <๐ŸŽœ>18<๐ŸŽœ> <๐ŸŽœ>19<๐ŸŽœ> <๐ŸŽœ>20<๐ŸŽœ> <๐ŸŽœ>21<๐ŸŽœ> <๐ŸŽœ>22<๐ŸŽœ> <๐ŸŽœ>23<๐ŸŽœ> <๐ŸŽœ>24<๐ŸŽœ> <๐ŸŽœ>25<๐ŸŽœ> <๐ŸŽœ>26<๐ŸŽœ> <๐ŸŽœ>27<๐ŸŽœ> <๐ŸŽœ>28<๐ŸŽœ>
<๐ŸŽœ> <๐ŸŽœ>function get_real_ipaddress() {<๐ŸŽœ> <๐ŸŽœ>if (!empty($_SERVER['HTTP_CLIENT_IP'])) {<๐ŸŽœ> <๐ŸŽœ>return $_SERVER['HTTP_CLIENT_IP'];<๐ŸŽœ> <๐ŸŽœ>} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {<๐ŸŽœ> <๐ŸŽœ>return $_SERVER['HTTP_X_FORWARDED_FOR'];<๐ŸŽœ> <๐ŸŽœ>}<๐ŸŽœ> <๐ŸŽœ>return $_SERVER['REMOTE_ADDR'];<๐ŸŽœ> <๐ŸŽœ>}<๐ŸŽœ> <๐ŸŽœ>function in_ip_range($ip, $ip_one, $ip_two = false) {<๐ŸŽœ> <๐ŸŽœ>if(!$ip_two) {<๐ŸŽœ> <๐ŸŽœ>return $ip_one === $ip;<๐ŸŽœ> <๐ŸŽœ>}<๐ŸŽœ> <๐ŸŽœ>return ip2long($ip_one) * -1 >= ip2long($ip) * -1 && ip2long($ip_two) * -1 <= ip2long($ip) * -1; } function validate_ip() { $ip = explode(':', get_real_ipaddress()); $ip = $ip[0]; if(in_ip_range($ip, '212.76.229.115', '212.76.229.120')) { return true; } else if(in_ip_range($ip, '194.78.4.66', '194.78.4.79')) { return true; } else if(in_ip_range($ip, '194.8.4.78', '194.8.4.78')) { return true; } else if(in_ip_range($ip, '0', '1')) { // local return true; } header('Location: http://www.bkjia.net'); }

I hope this article will be helpful to everyoneโ€™s PHP programming design. http://www.bkjia.com/PHPjc/977162.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/977162.htmlTechArticleHow to limit ip address range in php. Only ip addresses within the limited range can be accessed 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 function get_real_ipaddr...
Statement๏ผš
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