Home >Backend Development >PHP Tutorial >PHP limits frequent requests

PHP limits frequent requests

WBOY
WBOYOriginal
2016-07-29 09:00:553266browse
<?php header("Content-Type:text/html; charset=utf-8");
 

include_once("libs/RSA.class.php");
/**
 * 在线高频频繁提交解决方案
 * @author 周阳<305846826@qq.com>
 * @link http://www.jieone.com/
 */
class Token extends RSA{
	/**
	 * 客户端唯一ID
	 */
	private $date=0;
	
	private $clientID="abccccc";
	/**
	 * 系统access_token,即要保存于客户端的数据
	 */
	private function sys_access_token(){
		$array=array(
				'clientID'=>$this->clientID,
				'date'=>$this->date
				);
		$sys_access_token = json_encode($array);
		//RSA公钥加密
		return $this->public_encrypt($sys_access_token);
	}
	/**
	 * 保存access_token
	 */
	public function get_access_token(){
		
		 if(empty($_COOKIE['access_token'])){
			 $this->date=time()-100;
			return $this->sys_access_token(); 
		 }
		 return $_COOKIE['access_token'];
	}
	/**
	 * 保存access_token
	 */
	private function save_access_token(){
		
		setcookie("access_token",$this->sys_access_token());	
	}
	
	public function check(){
		
		$access_token=$this->get_access_token();
		
		//RSA私钥解密
		$access_token = $this->private_decrypt($access_token);
		
		//信息已被非法篡改
		if(empty($access_token)){
			return false;
		}
		
		$object=json_decode($access_token);

		//2秒只能ajax请求一次,可修改
		if ($object->date + 2 > time()){
			return false;
		}
		//保存access_token
		$this->date=time();
		$this->save_access_token();
		return true;	
	}
		
}

echo '<br><br><br><br><br><br><center><h1>频繁刷新试试</h1></center><br>';

$Token=new Token();
if(!$Token->check()){
	echo '<center><h2 style=" color:red">您请求过快,请稍后再试</h2></center><br><br><br>';
	exit();
}
echo '<center><h2 style=" color:green">访问正常</h2></center><br><br><br>';

echo "您当前access_token:".$Token->get_access_token();

PHP limits frequent requests

The above introduces PHP's restriction of frequent requests, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

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