Home Backend Development PHP Problem How to ban ip segment in php

How to ban ip segment in php

Oct 06, 2020 am 10:23 AM
ip segment php

php method to disable ip segments: 1. Disable a single ip, the code is [$ip=$_SERVER["REMOTE_ADDR"];]; 2. Disable the ip segment, the code is [$ip=$_SERVER[ "REMOTE_ADDR"];].

How to ban ip segment in php

How to ban ip segment in php:

This article describes the example of php banning access to a certain ip or ip address segment The method is shared with everyone for your reference. The specific analysis is as follows:

Because I haven’t studied Apache and nginx in depth yet, I wrote a small program in PHP that can disable addresses

When using it, just:

<?php include("banIP.php");?>
Copy after login

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're forbiden to view this page!");   
} 
echo "Your IP Address is:$ip,hello!"; 
?>
Copy after login

ban.dat file is as follows:

BEGIN: 
119.184.251.245 
127.0.0.1 
192.168.1.100
Copy after login

Disable an IP segment as follows:

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

Related learning recommendations: php programming(video)

The above is the detailed content of How to ban ip segment in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles