Results 1 to 8 of 8

Thread: AW Stats Code

  1. #1

    AW Stats Code

    Some code to spit out awstats config files from your Apache vhosts file ("hosts.conf") based on a template ("awstats.conf")
    PHP Code:
    /* * Generates awstats config files * LogFile="C:/Apache24/logs/mysite-access.log" * SiteDomain="" */
    $hosts file_get_contents("hosts.conf");$conf file_get_contents("awstats.conf");
    preg_match_all("@ServerName ([.\-a-z1-9]+)@"$hosts$matches);
    preg_match_all("@CustomLog logs/([-a-z1-9]+)-access.log combined@"$hosts$matches2);
    $matches[1] as $key => $domain){    $content sprintf("LogFile=\"C:/Apache24/logs/%s-access.log\"\nSiteDomain=\"%s\"\n%s"$matches2[1][$key], $domain$conf);    file_put_contents(sprintf("awstats.%s.conf"$domain), $content);}?>

  2. #2
    Where does the PHP code get placed? Is this a stand-alone script that is run with a cron job?

    What exactly does it spit out? Is it raw data?

    I have been looking for a way to automatically e-mail AwStats data one the first of every month.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin

  3. #3
    This is a script I wrote to generate the log configuration files based on what I had set up in my virtual hosts file (I renamed that file to hosts.conf). So it's a script to run one-time to save a lot of manual text editing.

    To automatically e-mail stats data on the first of every month, you can send the website access log file by email.

    You could set up a cron job to run a PHP script such as this:

    PHP Code:
    $stats file_get_contents("location of the access log file");
    mail('the destination email box''Latest Stats for' $_SERVER['HTTP_HOST'], $stats); 
    And give the PHP script a cryptic name, and locate it above the public HTML directory area for security purposes.

    Another idea would be to capture the HTML output of the AW Stats URL and send this via e-mail to a client. The above script may even work for this too. Otherwise, look at using CURL to capture the data.

    The visual stats URL is something like this:
    On Apache, the stats packages operate on the server access log files. And the stats script is run by a timer to process the raw log data. Then the user views the stats visually via a URL to a perl script in the cgi-bin.

  4. #4
    Interesting. I will have to try this.

    When I did some research on this topic several months ago, numerous articles and forum threads said that AwStats data could not be extracted and e-mailed.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin

  5. #5
    Another idea is to log into cPanel and find out the URL for the stats that you want to see. Then use the PHP Curl function with login credentials (username + password) to grab the HTML contents of this URL. This should work with shared hosting accounts where you don't get access to the server log files via PHP scripts.

    Maybe of some help?:
    PHP Code:
    class Connector{
    $cpanelUser;    public $cpanelPassword;    public $domain;    public $bcName;    private $curlDone;
        public function 
    tryCurl($url$pdata null$usecookies false) {        if (strpos($url':2082/') > 0str_replace('/www.''/'$url);        $c curl_init($url);        if (!empty($pdata)) {            foreach ($pdata as $k => $p$pdata[$k] = preg_replace('/^@/''\@'$p);            curl_setopt($cCURLOPT_POST1);            curl_setopt($cCURLOPT_POSTFIELDS$pdata);        }        if ($usecookies) {            curl_setopt($cCURLOPT_COOKIEJAR"cookies.txt");  //initiates cookie file if needed            curl_setopt($c, CURLOPT_COOKIEFILE, "cookies.txt");        }        curl_setopt($c, CURLOPT_USERPWD, $this->cpanelUser . ':' . $this->cpanelPassword);        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);        //curl_setopt($c, CURLOPT_FOLLOWLOCATION, 1);        $result = curl_exec($c);        if ($result === false) $result = curl_error($c); else $this->curlDone = true;        curl_close($c);        return $result;    }
    public function doCurl($url$pdata null$usecookies false) {    $tries 0;        $this->curlDone false;    do {            $result $this->tryCurl($url$pdata$usecookies);            if ($this->curlDone) break;            sleep(2); // Wait for 2 seconds            $tries++;    } while ($tries < 4);        return $result;    }} 

  6. #6
    And another way may be to automate the Internet Explorer browser on Windows using AutoIt - AutoItScript i.e. simulate how you would view your web stats and capture the web page source to send via email. And set up the script as a scheduled task.

  7. #7
    Also, with a GUI automation script, you could e-mail a screen capture image if the HTML doesn't render well for the remote recipient.

    btw. one reason for stats data being given restricted access is to combat referral spam where spammers inject their link to the stats (via automated page requests) that may be accessible by a search engine robot.

    On VPS or dedicated servers, it should be easy to get hold of the stats data to send in an e-mail with a cgi script.

  8. #8
    AW Stats Download Script

    Here is the workflow for the script development:

    * Login to cPanel using Curl
    * Download the HTML of the AW Stats page
    * Parse the required data
    * Produce the stats file to email to clients or whatever

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts