Home >Backend Development >PHP Tutorial >How to ban access to a certain IP or IP address range in PHP

How to ban access to a certain IP or IP address range in PHP

墨辰丷
墨辰丷Original
2018-06-13 09:59:312488browse

This article mainly introduces the method of prohibiting access to a certain IP or IP address range in PHP, involving the operation skills of server variables $_SERVER, files and strings. It has certain reference value. Friends in need can refer to it

The example in this article describes how PHP prohibits access to a certain IP or IP address range. The specific analysis is as follows:

Because I have not studied Apache and nginx in depth, I wrote a small program in PHP that can disable addresses
When using, just:

<?php include("banIP.php");?>

Disable a single ip as follows:

<?php 
//禁用ip地址 
$ip=$_SERVER["REMOTE_ADDR"]; 
$ban=file_get_contents("ban.dat"); 
if(stripos($ban,$ip)) 
{ 
  die("Your IP Address is:$ip,you&#39;re forbiden to view this page!");   
} 
echo "Your IP Address is:$ip,hello!"; 
?>

ban.dat file is as follows:

BEGIN: 
119.184.251.245 
127.0.0.1 
192.168.1.100

Disable ip segment As follows:

<?php 
//禁用ip地址 
$ip=$_SERVER["REMOTE_ADDR"]; 
while($ip[count($ip-1)]!=&#39;.&#39;)$ip=substr($ip,1, -1); //整理出ip段 
$ban=file_get_contents("ban.dat"); 
if(stripos($ban,$ip)) 
{ 
  die("U&#39;re forbiden to view this page!");   
} 
echo "Hello!"; 
?>

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Brief introduction to the method of implementing httpRequest in PHP

##PHP framework laravel installation and configuration instructions

How to operate directories and files in php

The above is the detailed content of How to ban access to a certain IP or IP address range in PHP. For more information, please follow other related articles on the PHP Chinese website!

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