View awstats outside of cPanel

From JumbaWiki

Jump to: navigation, search

Sometimes you might not want to give a client/customer (or their client) the cPanel login details to access the site statistics. Here's a way of making a PHP page on your site that displays the awstats outside of cpanel.

Please note: you will need to change the commented areas of the code for your domain, username, and password.

Contents

Updated version Dec 2008

This version behaves more like Awstats in cPanel, and lets you select the month to view.

<?php
 
// For this script to show Awstats graphics correctly, they must be uploaded to your server.
// The graphics from Awstats v6.8 are available from the external link on this wiki page, 
// otherwise you can download a copy of the Awstats package from http://awstats.sourceforge.net/
// Within the package is the directory wwwroot/icon/
// FTP all the folders in this directory to a folder on your server.
 
// To view the stats for an addon or subdomain, just append to the URL:
//  ?config=subdomain.yourdomain.com.au
 
 
// Required settings
$Domain = 'yourdomain.com.au'; // change this to your own domain
$UserId = 'username';          // change this to your cpanel username
$Secret = 'password';          // change this to your cpanel password
$ImageDir='awstats_images';    // location of the awstats graphics
 
// Optional settings
$DefaultLanguage = 'en';
$HideLanguages   = false;
$HideAwstatsLogo = false;
$CustomLogo      = '';
$CustomAltTitle  = '';
$CustomUrl       = '';
 
// ------------------------------------------------------
 
$SayDomain=(isset($_GET['config'])) ?  $_GET['config'] : $Domain;
if(0==count($_GET))
    { $qs="config=$Domain&lang=$DefaultLanguage&framename=mainright"; }
else
    {
    $qs = ''; 
    foreach ($_GET as $key=>$value) 
        { $value = urlencode(stripslashes($value)); $qs.="$key=$value&"; }
    $qs=substr($qs,0,-1);
    }
 
$Secret=rawurlencode($Secret);
$Stats = file_get_contents("http://$UserId:$Secret@$Domain:2082/awstats.pl?$qs");
$Stats=str_replace('<form name="FormDateFilter"', "<center><h2>Web statistics for $SayDomain</h2></center>\r\n<form name=\"FormDateFilter\"", $Stats);
$Stats=str_replace('awstats.pl', $_SERVER['PHP_SELF'], $Stats);
if(substr($ImageDir,-1)<>'/') $ImageDir.='/';
$Stats=str_replace('/images/awstats/', $ImageDir, $Stats);
$Stats=str_replace('framename=index', 'framename=mainright', $Stats);
$Stats=str_replace('name="framename" value="index"', 'name="framename" value="mainright"', $Stats);
$Stats=str_replace('target="mainright"', '', $Stats);
if($HideLanguages) $Stats=preg_replace('/<td align="right" rowspan="2">(<a .*<\/a>).*<br \/><a .*<\/td>/Us', '<td align="right" rowspan="2">$1</td>', $Stats, 1);
if($HideAwstatsLogo or $CustomLogo<>'')
    {
    $Img="<img src=\"$CustomLogo\" border=\"0\" alt=\"$CustomAltTitle\" title=\"$CustomAltTitle\" />";
    $Logo=(''==$CustomUrl) ? $Img : "<a href=\"$CustomUrl\" target=\"_blank\">$Img</a>";
    if(''==$CustomLogo) $Logo='&nbsp;';
    $Stats=preg_replace('/<td align="right" rowspan="2">(<a .*<\/a>)/Us', '<td align="right" rowspan="2">'.$Logo, $Stats, 1);
    }
echo $Stats;
 
?>

Updated version Jan 2007

Here is another PHP script that presents the awstats page for the current month, including graphics if you follow the instructions in the comments:

<?
 
//For this script to show graphics correctly, there must be a folder in the www root
//called "images/awstats"
//Download a copy of the awstats package from http://awstats.sourceforge.net/
//Within the package (version 6.6 as of writing) is the directory \awstats-6.6\wwwroot\icon\
//ftp all the folders in this directory to the images/awstats directory in the root
//of your website
//graphics on the stats page should be displayed correctly now
 
//Note: This will probably break when Jumba updates awstats to 6.6
//I'll fix it then :)
 
$domain = "domain"; //edit this variable to include your domain
$userid = "userid"; //edit this variable to include your cpanel userid
$secret = "password"; //edit this variable to include your cpanel password
 
$s = file("http://".$userid.":".$secret."@".$domain.":2082/awstats.pl?config=".$domain."&lang=en&framename=mainright");
 
//Remove update form at top of screen
//A hideous hack!
for ($key=49; $key<=89; $key++)
{
	$s[$key] = "";
}
 
//Turn array into a string
$stats = implode("", $s);
 
//Send to screen
echo "<b><center>Current Month Stats for $domain</center></b><p>";
echo $stats;
 
?>

Original version

This code was sourced from http://www.totallyphp.co.uk and shows statistics for the current month

<style type="text/css">
<!--
.aws_bodyl { }
.aws_border { background-color: #CCCCDD; padding: 1px 1px 1px 1px; margin-top: 0; margin-bottom: 0; }
.aws_title { font: 13px verdana, arial, helvetica, sans-serif; font-weight: bold; background-color: #CCCCDD; text-align: center; margin-top: 0; margin-bottom: 0; padding: 1px 1px 1px 1px; color: #000000; }
.aws_blank { font: 13px verdana, arial, helvetica, sans-serif; background-color: #CCCCDD; text-align: center; margin-bottom: 0; padding: 1px 1px 1px 1px; }
.aws_data { background-color: #FFFFFF; border-top-width: 1px; border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; }
.aws_formfield { font: 13px verdana, arial, helvetica; }
.aws_button { font-family: arial,verdana,helvetica, sans-serif; font-size: 12px; border: 1px solid #ccd7e0; background-image : url(/images/awstats/other/button.gif); }
th { border-color: #ECECEC; border-left-width: 0px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 1px; padding: 1px 2px 1px 1px; font: 11px verdana, arial, helvetica, sans-serif; text-align:center; color: #000000; }
th.aws { border-color: #ECECEC; border-left-width: 0px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 1px; padding: 1px 2px 1px 1px; font-size: 13px; font-weight: bold; }
td { border-color: #ECECEC; border-left-width: 0px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 1px; padding: 0px; font: 10px arial, helvetica, sans-serif; text-align:left; color: #000000; }
td.aws { border-color: #ECECEC; border-left-width: 0px; border-right-width: 1px; border-top-width: 0px; border-bottom-width: 1px; padding: 1px 1px 1px 1px; font: 11px verdana, arial, helvetica, sans-serif; text-align:left; color: #000000; }
td.awsm { border-left-width: 0px; border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; padding: 0px 0px 0px 0px; font: 11px verdana, arial, helvetica, sans-serif; text-align:left; color: #000000; }
//-->
</style>
<? 
$domain = "mydomain.com"; //edit this variable to include your domain
$username = "username"; //edit this variable to include your cpanel username
$password = "password"; //edit this variable to include your cpanel password
 
echo "<div class=\"newsheader\">Site Statistics for ".$domain."</div><br>
Please note that these statistics are indicatory only. They are taken from a server side, third party program, and are not guaranteed to be 100% accurate.<br><br>"; ?>
 
<?
file("http://".$username.":".$password."@".$domain.":2082/awstats.pl?config=".$domain."&lang=en&framename=mainright&update=1");
$t = file("http://".$username.":".$password."@".$domain.":2082/awstats.pl?config=".$domain."&lang=en&framename=mainright");
$r = implode("", $t);
$qqq = explode("<a name=\"month\">&nbsp;</a><br />", $r);
 
//$top = explode("<a name=\"who\">&nbsp;</a>", $qqq[1]);
$top = explode("<a name=\"errors\">&nbsp;</a><br />", $qqq[1]);
 
if(!$top[0]) echo "Error Has Occured";
else {
echo str_replace(array("Days of month ", "Days of week ", "Hours ", "/images/awstats", "a href"), array("Days of month (current month)", "Days of week (current month)", "Hours (current month)", "$domain/awstats","o "), $top[0]);
 
$previsits = explode("<a name=\"sessions\">&nbsp;</a><br />", $top[1]);
$visits = explode("<a name=\"filetypes\">&nbsp;</a><br />", $previsits[1]);
 
echo str_replace("Visits Duration", "Visits Duration (current month)", $visits[0]);
}
 
?>

See also

External links

Personal tools
Australian Photography | Unique Views of Reality | Daedalus | Pet Forums