PHP to retrieve cPanel Webalizer stats
PHP script to display your Webalizer stats
If you’re hosting your site on a cPanel-powered server, you’ll probably know it comes with a built-in stats app called Webalizer. Not that this is the best stats solution out there (I’d rather go for Google Analytics instead), but it is built-in, so you might want to give it a try.
Some time ago my friend Giacomo sent me this little PHP script that allows you to view your cPanel/Webalizer stats from your browser or to make them publicly visible (or maybe make it visible to the client without forcing him to log into cPanel).
Just paste the following code into a text file, save it as stats.php and upload it to your web server.
Viewing it in a browser will display your Webalizer stats and graphs.
<?php
$user = 'username'; //cpanel username
$pass = 'password'; //cpanel password
$url = 'www.yourdomain.com'; //do not include 'http://'
//retrieves the webalizer file, either .html or .png
function getFile($file) {
global $user, $pass, $url;
return file_get_contents("http://" . $user . ":" . $pass . "@" . $url . ":2082/tmp/" . $user . "/webalizer/" . $file);
}
//alters links, either .html or .png
function changeLinks($subject, $type) {
return preg_replace("/($type=")(?!http)(.*?)"/is","$1$PHP_SELF?$2"",$subject);
}
if(!empty($_SERVER['QUERY_STRING'])) {
//get file (whether png or html)
$page = getFile($_SERVER['QUERY_STRING']);
//if png, output appropriate header
if(strpos($_SERVER['QUERY_STRING'],'.png') !== false) {
header("Content-type: image/png");
}
//change the .png src(s)
else {
$page = changeLinks($page, 'src');
}
}
else {
//get index
$page = getFile('index.html');
//change links
$page = changeLinks($page, 'href');
//change the usage.png src
$page = changeLinks($page, 'src');
}
//output it
echo $page;
?>
Download the source code (1k ZIP)
This tutorial is provided as is. Please do not email me questions or support requests about the scripts contained in this page. Use the comments below instead. Thank you.
About this entry
You’re currently reading “PHP to retrieve cPanel Webalizer stats,” an entry on jek2kdotcom
- Posted on:
- 20.02.2009 @ 10am
- Categories:
- PHP
4 Comments | View
Come on, leave your feedback!