We need to modify our bandwidth usage (per user on our site). I would want to limit bandwidth per ip,,. Basically we want the users who connects to our webstie to only be able to (download @ 2.5Mbps or 250KB/per sec). While were able to cache and upload @ unlimited speed.. Right now we have no cap.


If you're concerned about fairness, I would not recommend limiting per user. It works as long as you have a low connection count, but eventually the sum of all limited connections exceeds your available bandwidth and the situation is as bad as without the limit.

There is a thing called Stochastic Fairness Queuing. It will prevent that a single connection starves and improve fairness. It identifies connections using a hash algorithm and serves them one packet at a time in round robin order.

In order for SFQ to work you have first to limit bandwidth, to move the queue to your machine where you can control it. Here is a very simple setup I often use:

$TC qdisc del dev eth0 root handle 1
$TC qdisc add dev eth0 root handle 1: htb default 20 r2q 50
$TC class add dev eth0 parent 1: classid 1:1 htb rate 10mbit ceil 10mbit
$TC qdisc add dev eth0 parent 1:1 handle 110: sfq perturb 10
$TC filter add dev eth0 parent 1: prio 3 protocol all u32 match u32 0 0 flowid 1:1

If you find this useful please explain your motivation and I can provide more complex setups that'll suit your purpose.


If you are talking about HTTP traffic, you can direct the users to a proxy such as squid. Squid can be configured to limit the bandwidth usage per IP. You can use delay pools in configuration parameters. You can have a look at this page. You can find many other pages on the internet.

I think it can be also configured to limit the bandwidth per user when enabling authentication.


bandwidth is for download and page view

I think is better you create extra layer on your project and then add some size after any request ( like page view ) and then calculate size of response and sum with traffic of user

if user use more than bandwidth then make bandwidth finish response


This can be achieved using readfile() where the data is split into chunks. You can set the chunk size and place a sleep() to control how fast chunks are sent. I have code which is not well tested, but could point you in the right direction, however reading user contributed notes for readfile and fpassthru on php net will help.


A basic technique that sends out a downloadable file at limited bandwidth is to replace the fpassthru or readfile call with a loop that reads the file in chunks with fread and a sleep or usleep call to throttle the rate.

But this only takes care of downloading a file, it doesn't affect reading static files that don't go through PHP, or reading the other output of PHP pages. You'll also need a central point where you remember how many concurrent connections each IP address has, and distribute their allocated bandwidth among them.

Probably it's best not to do this in PHP at all, but at the Apache or kernel level, assuming you're not on shared hosting of course. For Apache there is the bwmod extension.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.