php bandwidth used script

gtakiller

New Member
Messages
1
Reaction score
0
Points
0
i will show how to make a bandwidth used bar

step 1 make a new php file eg bandwidth.php

step 2 add
PHP:
<?php
$sAuth = base64_encode("username:pass");  //your username:password for cpanel
$domain = "www.domain.com"; //domain of your website
$total = 10000; //amount of bandwidth you get in mb
$cpanelport = 2082; //port of cpanel default is 2082

$hSocket = fsockopen($domain,$cpanelport);
if(!$hSocket) {
    die("Connection Failed.");
} else {
    fputs($hSocket, "POST /frontend/x3/index.html HTTP/1.0\r\n"); //based on the x3 theme of cpanel
    fputs($hSocket, "Host: $domain\r\n");
    fputs($hSocket, "Authorization: Basic $sAuth\r\n");
    fputs($hSocket, "Connection: Close\r\n");
    fputs($hSocket, "\r\n");
    $printNext = false;
    $bw = 0;
    while(!feof($hSocket)) {
        $response = fgets($hSocket, 4096);
        $response = strip_tags($response);
        $response = addslashes($response);
        $pos = strpos($response, 'Monthly Bandwidth Transfer');
        if($pos > 0) {
            $response = explode("/", $response);
            $t = array_pop($response);
            $t = array_shift($response);
            $response = explode("Monthly Bandwidth Transfer", $response[0]);
            $t = array_shift($response);
            break;
        }
    }
}
fclose($hSocket);
header("Content-type: image/png");
if(!$total) {
    $total = 100;
}
function drawBar($bandwidth, $total) {
    //$total=$bandwidth+3.14;
    $barWidth = 200; //set this for bar width
    $barHeight = 16; //set this for bar height
    $fontSize = 2; //HTML Font Size
    $imgWidth = $barWidth; //Keep this
    $imgHeight = $barHeight; //Keep this
    $image = imagecreate($imgWidth, $imgHeight);
    $back = ImageColorAllocate($image, 255, 255, 255);
    $border = ImageColorAllocate($image, 0, 0, 0);
    $fill = ImageColorAllocate($image, 255, 139, 0);
    $text = ImageColorAllocate($image, 0, 71, 255);
    ImageFilledRectangle($image, 0, 0, $barWidth, $barHeight - 1, $back);
    $adjBar = (float)(($bandwidth / $total) * ($barWidth - 1));
    ImageFilledRectangle($image, 1, 1, $adjBar, ($barHeight - 2), $fill);
    ImageRectangle($image, 0, 0, $barWidth - 1, $barHeight - 1, $border);
    $PerCent = (float)(($bandwidth / $total) * 100);
    $myText = (string )("Usage: " . round($bandwidth, 2) . "MB/" . $total . "MB");
    $textLength = strlen($myText);
    $textLeft = (($barWidth - 1) / 2) - (($textLength / 2) * imagefontwidth($fontSize));
    $textTop = ($barHeight - imagefontheight($fontSize)) / 2;
    ImageString($image, $fontSize, $textLeft, $textTop, $myText, $text);
    imagePNG($image);
    imagedestroy($image);
}
drawBar((float)$response[0], $total);
?>

step 3 change $sAuth = base64_encode("username:pass"); $domain = "www.domain.com"; line to your info

step 4 save and upload

to add the image to html flie just use
<img src='bandwidth.php' alt='Bandwidth Usage' />"



bandwidth.php
 
Last edited:
Top