Location: PHPKode > projects > phpBandwidth > phpbandwidth/userstats.php
<?
// phpbandwidth by Alexander Wigen
include("db_config.php");
include("db_$db[type].php");
include("user_functions.php");
include("header.php");

$conn = db_connect();
$users = db_get_users($conn);
$bandwidth = fill_bandwidth_array($conn, $uid);
echo "User stats for $users[$uid] in ".date("F Y", mktime(0,0,0,$current_month,$day,$current_year))."<br>";

if ($current_month == 1)
	echo "<a href=\"$PHP_SELF?uid=$uid&current_month=12&current_year=".($current_year - 1)."\">Prev</a>";
else
	echo "<a href=\"$PHP_SELF?uid=$uid&current_month=". ($current_month - 1) ."&current_year=$current_year\">Prev</a>";
echo " Month";
if ($current_month != date('m') || $current_year != date('Y'))
	if ($current_month == 12)
		echo "<a href=\"$PHP_SELF?uid=$uid&current_month=1&current_year=". ($current_year + 1) ."\"> Next</a>";
	else
		echo "<a href=\"$PHP_SELF?uid=$uid&current_month=".($current_month + 1)."&current_year=$current_year\"> Next</a>";

	
echo "<br>";
	
function daily_stats(){
	global $uid, $current_month, $current_year, $bandwidth, $uid;
if ($current_month == date('m') && $current_year == date('Y'))
{
	$days_left = date("t") - date("d") +1;
	$avr_day = number_format($bandwidth[left] / $days_left, 1,'.','');
	if ($avr_day < 0)
		$avr_day = 0;
	$avr_today = number_format($avr_day - $bandwidth[today_total], 1, '.','');
	if ($avr_day) // If we have used more than our daily quota, set prosent_today to 100%
		$prosent_today = (($bandwidth[today_total])/$avr_day)*100;
		else
		$prosent_today = 100;
	
	$prosent_today .= "%";
	
	$data .= "
	<table border=0 bgcolor=\"#aaaaaa\">
	 <tr>
	  <td align=center>
	   <b>Today</b>
	   <br>
	
	";
	$data .=  html_bar_day(220, $prosent_today , 20);
	$data .=  "
	   <table border=0>
	    <tr>
	     <td>Total</td>
	     <td align=right>".number_format($bandwidth[today_total], 3 ,'.','') . " Mb</td>
	    </tr>
	    <tr>
	     <td>In</td>
	     <td align=right>".number_format($bandwidth[today_in], 3 ,'.','') . " Mb</td>
	    </tr>
	    <tr>
	     <td>Out</td>
	     <td align=right>".number_format($bandwidth[today_out], 3 ,'.','') . " Mb</td>
	    </tr>
	    <tr><td colspan=\"2\"><a href=\"bw_image_html.php?uid=$uid&year=".date("Y")."&month=".date("m")."&day=".date("d")."&width=800&heigth=200"."\">
	    <img border=0 width=210 height=50 src=\"bw_image.php?uid=$uid&year=".date("Y")."&month=".date("m")."&day=".date("d")."&width=210&heigth=50\"></a>
	    </td></tr>
	   </table>
	  </td>
	 </tr>
	</table>
	<br>
	$days_left days left of this month<br>";
	if ($avr_day)
	$data .=  "You can use $avr_day Mb per day<br>";
	if ($avr_today > 0)
		$data .=  "There is $avr_today Mb left of todays quota";
		else
		$data .=  "You are <font color=\"red\">" . -$avr_today." Mb</font> over today";
return $data;
}
}

echo "
</td></tr></table>";
echo "<table border=0 cellpadding=\"3\" cellspacing=\"15\">
<tr>
	<td valign=top>" . monthly_stats() . "</td>
	<td valign=top>". daily_stats() ."</td>
</tr>
</table>";
function monthly_stats(){
	global $bandwidth, $current_year, $current_month,$uid;
		$prosent = ($bandwidth[total]/$bandwidth[limit])*100;
	if ($avr_day) // If we have used more than our daily quota, set prosent_today to 100%
		$prosent_today = (($bandwidth[today_total])/$avr_day)*100;
		else
		$prosent_today = 100;
	
	$prosent_today .= "%";
	
	// If not in current month display progress bar as 100%
	if ($current_month == date('m') && $current_year == date('Y'))
		$data = html_bar_month(220, $prosent . "%", 40);
	else	
		$data = html_bar(220, $prosent . "%", 40);	
	
	$data .= "
	<br>
	You have used <b>" . number_format($bandwidth[total], 0 ,'.','') . " Mb</b><br>
	That's ". number_format($prosent, 0 ,'','') ."%
	</center>
	<br>";

	$data .= "<b>This Month</b><br>
	<table border=0>
	<tr>
	<td align=right>In</font></td>
	<td>".number_format($bandwidth[total_in], 1 ,'.','') . " Mb</font></td>
	</tr><tr>
	<td align=right>Out</font></td>
	<td>". number_format($bandwidth[total_out], 1 ,'.','') . " Mb</font></td>
	</tr><tr>
	<td align=right>Limit</td>
	<td>".number_format($bandwidth[limit], 0 ,'.','') . " Mb</td>
	</tr><tr>
	<td align=right>";
	if ($bandwidth[left] < 0)
		$data .= "<font color=\"red\">Over</font>";
		else
		$data .= "<font color=\"green\">Left</font>";
	$data .= "</td>
	<td><b>";
	if ($bandwidth[left] < 0)
		$data .= "<font color=\"red\">". - number_format(($bandwidth[left]), 0,'.','') . " Mb</font>";
		else
		$data .= "<font color=\"green\">".number_format(($bandwidth[left]), 0,'.','') . " Mb</font>";
	$data .=  "</b></td>
	</tr>
	</table>";
	return $data;
}

echo "<br><a href=\"userstats_daily.php?uid=$uid&current_month=$current_month&current_year=$current_year\">Click here for Daily Download Stats!</a>";
?>
<br>
<a href="index.php">Back</a>
Return current item: phpBandwidth