3

I want to set the bandwidth to the webpages using php.

I got some php code to set the bandwidth for downloading files but i want to set the bandwidth to the web pages.

Is it possible to set the bandwidth to the web pages using php?

If possible, please give me the sample code or working link for reference.

The below code is to sent the bandwidth for downloading files

set_time_limit(0);
$filedownload = "files/abc.exe";
$time = 10000;
$obytes = 150*1024; //150k download speed restriction
$fd = fopen ($filedownload, "rb");
while (!feof ($fd)) {
    list($usec, $sec) = explode(" ", microtime());
    $time_start = $usec + $sec;
    $bytes = ceil($obytes/100);
    echo fread($fd, $bytes);
    flush();

    if($time < 10000) usleep(10000-$time);
    $i++;
    list($usec, $sec) = explode(" ", microtime());
    $time_end = $usec + $sec;
    $time =ceil(($time_end - $time_start)*1000000)+10;
}
fclose ($fd);
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • I think you could use the same way by buffering the output. Take a look at [`ob_start()`](http://pl1.php.net/manual/en/function.ob-start.php) and related functions. – Voitcus Jun 07 '13 at 06:47
  • Hi Thanks for your reply. Then i used ob_start() and ob_get_length() in my code . I got some value as 68732. But how to limit the bandwidth using ob_start() and related functions – user2462414 Jun 07 '13 at 07:14
  • Well, I've never done this, but you can set a chunk size, maybe this will help. Now I'm not sure if it is possible, but maybe you could send the page in parts like you do with the file. – Voitcus Jun 07 '13 at 07:46

1 Answers1

0

I found the coding for this question

$speed = 10;

ob_start();

include(filename.php);
$now = time();
foreach(str_split(ob_get_clean(), $speed*1024) as $chunk)
{
    echo $chunk;
    flush();
    $now++;
    while($now > time())
    {
        usleep(1000000);
    }
} 
  • it's not really bandwidth limitation. It splits the output in small chunks and sends it part by part with a little delay to the client, but the single parts are sent with full speed. Also images and other resources will not be limited. What's the reason you need it for? Maybe another solution is much better for your purposes. – Karl Adler Jun 07 '13 at 09:12
  • We need to set the bandwidth limitation to each and every customer who are using their website which is launched in our server. So i want to limit the bandwidth based on the customer and pay. – user2462414 Jun 07 '13 at 09:31