从被淹没防止PHP脚本从被淹没防止PHP脚本(Prevent PHP script from bei

2019-05-13 12:58发布

我想阻止我的剧本,被淹没 - 如果用户按下F5正在执行每一次的脚本。

我想从这个阻止,并允许每两秒钟就有一个脚本执行,是否有任何解决方案?

Answer 1:

您可以使用内存缓存来做到这一点..

简单演示脚本

$memcache = new Memcache ();
$memcache->connect ( 'localhost', 11211 );
$runtime = $memcache->get ( 'floodControl' );

if ((time () - $runtime) < 2) {
    die ( "Die! Die! Die!" );
} 

else {
    echo "Welcome";
    $memcache->set ( "floodControl", time () );
}

这仅仅是一个示例代码..也有其他的事情要考虑,如

A.更好的IP地址检测(代理,TOR)

B.当前操作

C.每分钟等最长执行...

最大洪水等之后D.禁止用户

编辑1 -改良版

用法

$flood = new FloodDetection();
$flood->check();

echo "Welcome" ;

class FloodDetection {
    const HOST = "localhost";
    const PORT = 11211;
    private $memcache;
    private $ipAddress;

    private $timeLimitUser = array (
            "DEFAULT" => 2,
            "CHAT" => 3,
            "LOGIN" => 4 
    );
    private $timeLimitProcess = array (
            "DEFAULT" => 0.1,
            "CHAT" => 1.5,
            "LOGIN" => 0.1 
    );

    function __construct() {
        $this->memcache = new Memcache ();
        $this->memcache->connect ( self::HOST, self::PORT );
    }

    function addUserlimit($key, $time) {
        $this->timeLimitUser [$key] = $time;
    }

    function addProcesslimit($key, $time) {
        $this->timeLimitProcess [$key] = $time;
    }

    public function quickIP() {
        return (empty ( $_SERVER ['HTTP_CLIENT_IP'] ) ? (empty ( $_SERVER ['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER ['REMOTE_ADDR'] : $_SERVER ['HTTP_X_FORWARDED_FOR']) : $_SERVER ['HTTP_CLIENT_IP']);
    }

    public function check($action = "DEFAULT") {
        $ip = $this->quickIP ();
        $ipKey = "flood" . $action . sha1 ( $ip );

        $runtime = $this->memcache->get ( 'floodControl' );
        $iptime = $this->memcache->get ( $ipKey );

        $limitUser = isset ( $this->timeLimitUser [$action] ) ? $this->timeLimitUser [$action] : $this->timeLimitUser ['DEFAULT'];
        $limitProcess = isset ( $this->timeLimitProcess [$action] ) ? $this->timeLimitProcess [$action] : $this->timeLimitProcess ['DEFAULT'];

        if ((microtime ( true ) - $iptime) < $limitUser) {
            print ("Die! Die! Die! $ip") ;
            exit ();
        }

        // Limit All request
        if ((microtime ( true ) - $runtime) < $limitProcess) {
            print ("All of you Die! Die! Die! $ip") ;
            exit ();
        }

        $this->memcache->set ( "floodControl", microtime ( true ) );
        $this->memcache->set ( $ipKey, microtime ( true ) );
    }

}


Answer 2:

  1. 存储脚本的最后执行时间在一个数据库或文件。
  2. 从该文件/数据库中读取并比较当前时间。
  3. 如果差异在2秒,终止脚本。
  4. 否则,继续正常。


Answer 3:

您可以使用Cookie(可关闭),所以不是一个好主意,或者你可以用店里他的IP地址在数据库中,所以如果有更多的,那么X来自同一IP地址尝试那么不执行代码,只是一个如果else语句,你将需要一个表,IP地址的请求时间,尝试次数

如果你不想使用数据库,那么你可以使用下面的代码

$file = "file.txt";
$file_content = file_get_contents($file);
$fh = fopen($file, 'w') or die("could not open file");
$now = time();
if($now - $file_content > 60){
// your code here
fwrite($fh, $now);
}else{
echo "Try again later";
}
fclose($fh);

但在这种情况下,它不会为每个访问者,而是对所有的人(所以说用户A来了,执行脚本,用户B将无法直到60秒传递到执行它。



Answer 4:

最好的办法是存储在服务器端的时间。 如果让客户端上的信息,这将是通过传球容易。

我会为例如保存时间戳在表中。 这种投入和检查垃圾邮件对你的脚本。 而且很容易设置耐性。



Answer 5:

请使用APC缓存或mencache来存储信息存储到数据库或从文件中读取,我相信是时候/资源消耗



文章来源: Prevent PHP script from being flooded