Home  >  Article  >  Backend Development  >  PHP simply implements blocking access for users in the specified IP range, phpip_PHP tutorial

PHP simply implements blocking access for users in the specified IP range, phpip_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:55:181047browse

php simply implements blocking the access of users in the specified IP range. phpip

During this period, I always see some frequent users of unknown browsers and unknown operating systems in the statistical tools. The requests and IPs are all in the same segment, which makes me have some questions. Are these users considered valid users? There are no records of website visits by browsers or operating systems. After several days of analysis, I confirmed that these visits were not made by people but by machines or others collecting content from my site. I came up with the idea of ​​blocking this IP address.

Export the user data without browser records in the statistics and take a look. Blocking IPs is no longer practical. There are more than a hundred IPs, but if you look carefully, it is not difficult to see that these IPs are concentrated in certain IP blocks. Find a way to block users in the entire IP range, which may cause some accidental killings but can ensure that the website traffic is clean.

Okay, I won’t talk nonsense anymore. Below is an introduction to my implementation ideas and code

I want to block the first two ends of the IP segment 111.11.11.11. I want to block access to this IP segment starting with 111.11

My implementation idea is to store the IPs to be blocked in an array and then use the obtained user IPs to match them in the array. If they are in the array, they will be blocked, and if they are not in the array, they will be allowed.

Let’s serve. I wrote a function. It’s very rough. Masters, please don’t complain

/** 
 * 屏蔽IP段 
 */ 
function killIp($ip){ 
  $return = false; 
  $ip1 = array('111', '112', '102', '114'); 
  $ip2 = array('1', '2', '3', '4', '5'); 
  $temp = explode('.', $ip); 
  if (in_array($temp[0], $ip1) && in_array($temp[1], $ip2)) { 
    $return = true; 
  } 
  return $return; 
} 

The above is the entire content of this article, I hope you all like it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/992549.htmlTechArticlephp simply implements blocking the access of users in the specified IP range. phpip has always seen some in the statistical tools during this period. Frequent requests from users with unknown browsers and unknown operating systems and the IPs are all in the same place...
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