Home  >  Article  >  Backend Development  >  PHP blocks multiple IPs from accessing your website

PHP blocks multiple IPs from accessing your website

WBOY
WBOYOriginal
2016-07-25 08:42:40916browse

This code snippet allows you to block certain IP addresses from accessing your website

  1. if ( !file_exists('blocked_ips.txt') ) {
  2. $deny_ips = array(
  3. '127.0.0.1',
  4. '192.168.1.1',
  5. '83.76.27.9',
  6. '192.168.1.163'
  7. );
  8. } else {
  9. $deny_ips = file('blocked_ips.txt');
  10. }
  11. // read user ip adress:
  12. $ip = isset($_SERVER['REMOTE_ADDR']) ? trim($_SERVER['REMOTE_ADDR']) : '';
  13. // search current IP in $deny_ips array
  14. if ( (array_search($ip, $deny_ips ))!== FALSE ) {
  15. // address is blocked:
  16. echo 'Your IP adress ('.$ip.') was blocked!';
  17. exit;
  18. }
Copy code

Multiple, PHP


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