2

我想防止我的脚本被淹没 - 如果用户按 F5,它每次都在执行脚本。

我想防止这种情况并允许每 2 秒执行一个脚本,有什么解决方案吗?

4

5 回答 5

16

您可以使用 memcache 来执行此操作..

简单的演示脚本

$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地址检测(Proxy、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 ) );
    }

}
于 2012-04-14T16:50:10.763 回答
4
  1. 将脚本的最后执行时间存储在数据库或文件中。
  2. 从该文件/数据库中读取并与当前时间进行比较。
  3. 如果差值小于 2 秒,则终止脚本。
  4. 否则,继续正常。
于 2012-04-14T16:39:40.807 回答
2

您可以使用 cookie(可以禁用)所以这不是一个好主意,或者您可以使用将他的 IP 地址存储在数据库中,所以如果 X 尝试从同一个 IP 地址尝试更多,则不要执行代码,只是一个if 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 秒过去。

于 2012-04-14T16:43:09.717 回答
0

最好的方法是将时间存储在服务器端。如果您将信息留在客户端,则很容易绕过。

例如,我会将时间戳保存在表中。该输入并检查是否向您的脚本发送垃圾邮件。并且很容易设置公差。

于 2012-04-14T16:43:56.630 回答
0

使用 apc 缓存或 mencache 将信息存储到数据库或从文件中读取我认为是时间/资源消耗

于 2012-04-14T18:07:27.940 回答