如何限制用户连接 [英] How to Limit Connections for user

查看:111
本文介绍了如何限制用户连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出将被用于从一些网站它们不具有访问下载文件的下载脚本。我也有提供简历的支持,但作为IDM和许多新的下载管理器使用8-16连接,甚至更多。我发现大多数用户使用的是20-25 conections为每个文件。我必须提供无限制的下载文件,速度也不过限制了每个文件的连接。我能减缓负载和用户不必与一个问题。

I am making a download script which will be used to download files from some sites they don't have access to. I also have to provide resume support but as IDM and many new download managers use 8-16 connection or even more. I found most of the users are using 20-25 conections for each file. I have to provide unlimited file downloads and speed also but limit connections for every file. I can slow down the load and the users don't have a problem with that.

我很困惑如何做到这一点。它可以完成使用htaceess?此外,我想我可以通过一些连续的日志做,但我仍然感到困惑。

I am confused how to do it. Can it done using htaceess? Also I thought I could do it by making some continuous log but I am still confused.

推荐答案

我会强烈建议 limitipconn.c Apache服务器上。

I would strongly recommend limitipconn.c on an Apache server.

http://dominia.org/djao/limitipconn.html

用法的一个例子是

An example of usage would be

<IfModule mod_limitipconn.c>
     <Location /your-download-directory>
          MaxConnPerIP 3
     </Location>
</IfModule>


如果您preFER解决这一问题用PHP和一个数据库(如MySQL本例中使用)


If you prefer solving this problem with PHP and a database (such as MySQL used in this example)

<?php
// Allow this amount of concurrent connections
$max_connections = 100;

// User's IP
$user_ip = $_SERVER['REMOTE_ADDR'];

// Sanitized file name
$current_download = mysql_real_escape_string( 'download-file.zip' );

$query_count = mysql_query( "SELECT COUNT(*) FROM connections WHERE user_ip = '{$user_ip}'" );

// Amount of concurrent connections this user has running
list( $current_connections ) = mysql_fetch_array( $current_connections_query );

if( $current_connections < $max_downloads ) {
     // Insert this connection to database
     mysql_query( "INSERT INTO connections (user_ip, current_download) VALUES('{$user_ip}','{$current_download}')" );

     // This will remove this connection from the database when script finished
     register_shutdown_function('download_ended');

     // your code to download file goes here ...
}

else {
     // your code to run when user has maximum concurrent connections
     exit;
}

// Removes download session from database
function download_ended() {
     global $current_download, $user_ip;

     // Session complete, subtract one
     mysql_query( "DELETE FROM connections WHERE current_download = '{$current_download}' AND user_ip = '{$user_ip}' LIMIT 1" );
}
?>

这篇关于如何限制用户连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆