search
Homephp教程php手册用php代码限制国内IP访问我们网站

这篇文章主要介绍了用php代码限制国内IP访问我们网站,需要的朋友可以参考下

利用淘宝的IP接口来判断IP,是否是国内的ip,是国内(CN)的就不允许访问。

$ip = $_SERVER['REMOTE_ADDR']; $content = file_get_contents(‘?ip='.$ip); $banned = json_decode(trim($content), true); $lan = strtolower($_SERVER['HTTP_ACCEPT_LANGUAGE']); if((!empty($banned['data']['country_id']) && $banned['data']['country_id'] == ‘CN') || strstr($lan, ‘zh')) { header(“HTTP/1.0 404 Not Found”); echo ‘HTTP/1.0 404 Not Found'; exit; }

同时发现一篇好文章: 《Redis 来限制高并发 php代码实例》

Redis本质上也是一种键值数据库的,但它在保持键值数据库简单快捷特点的同时,又吸收了部分关系数据库的优点。从而使它的位置处于关系数据库和键值数据库之间。Redis不仅能保存Strings类型的数据,还能保存Lists类型(有序)和Sets类型(无序)的数据,而且还能完成排序(SORT) 等高级功能,在实现INCR,,SETNX等功能的时候,保证了其操作的原子性,除此以外,还支持主从复制等功能。
Redis 来限制高并发
php代码实例

$redis->setnx(‘lock:hot_items', true)尝试创建一个key作为”锁”.若key已存在,setnx不会做任何动作且返回值为false,所以只有一个客户端会返回true值进入if语句更新缓存. $redis = new redis(); $redis_key = ‘lock:hot_items'; $clock_expire_time = $redis->get($redis_key); if(!empty($clock_expire_time) && time() > intval($clock_expire_time)) { //解除当前Redis锁 $redis->delete($redis_key); } if($redis->setnx($redis_key, time() + 3) !== true) { echo ‘高并发有冲突'; } //操作你的代码, 同一时刻就一个人访问该代码了 //解除当前Redis锁 $redis->delete($redis_key);

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software