Language PHP : Hypertext Preprocessor

 Charge moyenne sur 1mn : 2.26 Charge moyenne sur 5mn : 0.96 Charge moyenne sur 15mn : 0.73


PHP

PHP is a popular general-purpose scripting language that is particularly suited for web development.

Fast, flexible and pragmatic, PHP is made for any blog or for all the most popular sites in the world.

The word PHP or programming language contained in web pages and executed on servers, they return the result directly to the client who can never see the source. Allows you to create dynamic web pages.

PHP.Net © 2001-2020 The PHP Group: Language Reference

Predefined PHP classes to make our life easier :)






Site user blocks : Account info / user rights / summary

Identify yourself

Authenticate with your ZW3B account and take advantage of member services.

Username :
Password :
  
  

Show AWStats Statistics in PHP

  • Show AWStats Statistics in PHP
Show AWStats Statistics in PHP : Gets main AWStats variables to display web statistics in a PHP script / page - Afficher les statistiques AWStats en PHP : Récupère des variables principales AWStats pour les afficher les statistiques Web dans un script/page PHP.

Informations :

Dates
  • Publish : : Friday 22 january 2021
  • Modification : Friday 22 january 2021

Share :

J'ai écris un script PHP qui récupère quelques variables sur la page AWStats d'un site Web (les statistiques Web).

Le script est le suivant :

Récupère les variables principale déjà analysées, auditées grâce au script AWStats grâce à la fonction awstats_resume

Script avec 131 lignes

001function awstats_resume($website, $year, $month = null) {
002    
003    $month = isset($month) ? $month : 'all';
004    
005    $directoryTmp = '/var/pro/web_sites/lab3w_fr/admin/tmp/';
006    $file_year = $website.'-'.$year;
007    $file = 'awstats_'.$file_year.'.php';
008    $secondes = '1800'; // 30min
009    /*
010    echo '<br/>COURANT : '.mktime();
011    echo '<br/>MODIFIE   : '.filemtime($directoryTmp.$file).'';
012    echo '<br/>LE CALCUL   : '.((filemtime($directoryTmp.$file) + $secondes) - mktime()).'';
013    echo '<br/>A MODIF   : '.(filemtime($directoryTmp.$file) + $secondes).'';
014    
015    echo '<br/>'.$directoryTmp.$file;
016    */
017    
018    if( (filemtime($directoryTmp.$file) + $secondes) <= mktime()) {
019        //echo '<br/>'.'On reecrit le fichier';
020    
021    //exit;
022        
023    $url ='http://www.zw3b.fr/pub/AWStats/awstats.pl?framename=mainright&month='.$month.'&year='.$year.'&output=main&config='.$website.'';
024    $contents = file_get_contents($url);
025    
026    if(isset($contents) && !empty($contents)):
027    if(preg_match_all("/.{1,$s}(?=W+)/", $contents, $lines) !== false) {
028        
029        $result['config']['caption'] = 'Awstats';
030        $result['config']['value'] = $website;
031        
032        $find_log = false;
033        
034        $find_report = false;
035        $line_report = null;
036        
037        $find_stats = false;
038        $line_stats = null;
039        
040        for ($i=0; $i < count($lines[0]); $i++) {
041            
042            
043            if($find_report == false && preg_match("/Reported period/", $lines[0][$i], $matches)) {
044                //echo '<br/>LINE Reported period '.$i;
045                
046                $find_log = true;
047                
048                //$find_report = true;
049                $line_report = $i;
050                
051                preg_match_all("/(.*)</td>/Ui", $lines[0][$i], $period);
052                
053                $result['period']['caption'] = trim(str_replace(array('<tr bgcolor="#ECECEC">', '<td class="aws">'), ' ', $period[1][0]));
054                
055            }
056
057            if($line_report !== null) {
058            
059                preg_match_all("/(.*)</td>/Ui", $lines[0][($line_report+1)], $title);
060            
061                $result['period']['value'] = trim($title[1][0]);
062                
063                $line_report = null;
064            }
065            
066            if($find_stats == false && preg_match("/Unique visitors/", $lines[0][$i], $matches)) {
067                //echo '<br/>LINE STATS '.$i;
068                
069                $find_stats = true;
070                $line_stats = $i;
071                
072                preg_match_all("/<td(.*)>(.*)</td>/Ui", $lines[0][$i], $caption);
073                
074                $result['UV']['caption'] = trim($caption[2][1]);
075                $result['NV']['caption'] = trim($caption[2][2]);
076                $result['P']['caption'] = trim($caption[2][3]);
077                $result['H']['caption'] = trim($caption[2][4]);
078                $result['B']['caption'] = trim($caption[2][5]);
079                
080            }
081            
082            if($line_stats !== null) {
083                
084                preg_match_all("/<td(.*)>(.*)</td>/Ui", $lines[0][($line_stats+1)], $value);
085                
086                $pattern = array('
087', '<b>', '</b>', '\'', '<td>', '</td>', '<= ', 'Exact value not available in &apos;Year&apos; view');
088                $replace = array(' ', '', '', '&apos;', '', '', '', '');
089                
090                $value[0][1] = trim(str_replace($pattern, $replace, $value[0][1]));
091                $result['UV']['value'] = explode(' ', $value[0][1]);
092                
093                $value[0][2] = trim(str_replace($pattern, $replace, $value[0][2]));
094                $result['NV']['value'] = explode(' ', $value[0][2]);
095                
096                $value[0][3] = trim(str_replace($pattern, $replace, $value[0][3]));
097                $result['P']['value'] = explode(' ', $value[0][3]);
098                
099                $value[0][4] = trim(str_replace($pattern, $replace, $value[0][4]));
100                $result['H']['value'] = explode(' ', $value[0][4]);
101                
102                $value[0][5] = trim(str_replace($pattern, $replace, $value[0][5]));
103                $result['B']['value'] = explode(' ', $value[0][5]);
104                
105                $line_stats = null;
106            }
107            
108            
109            //echo "[$i] {$lines[0][$i]}\n";
110        }
111        
112        
113        
114        if($find_log == true && isset($result) && is_array($result) && $year == date('Y') )
115            awstats_resume_write($result, $year);
116        
117        }
118        
119        endif;
120        
121        return isset($result) ? $result : null;
122        
123        
124    }
125    
126    
127}
128
129# Exemple pour voir le résultat
130//$awstats =  awstats_resume('www.zw3b.fr', '2021', $month = null);
131

Cette function awstats_resume récupère des variables sur la page concernant tout les mois de l'année 2021 du site Web www.zw3b.fr.
L'URL est : AWSTtats/awstats.pl?framename=mainright&month=all&year=2021&output=main&config=www.zw3b.fr

Je récupère les variables : Unique visitors, Number of visits, Pages Views, Hits et Bandwidth.

En ligne , grâce à la fonction awstats_resume_write je stock les variables dans un fichier pour pouvoir les récupérer facilement (et surtout ne pas le faire si l'année est passée (n'est pas l'année courante).

Ecriture dans un fichier grâce à la fonction awstats_resume_write

Script avec 69 lignes


001function awstats_resume_write($result, $year = null) {
002
003    $year = isset($year) && !empty($year) ?$year : date('Y');
004    
005    $directoryTmp = '/var/pro/web_sites/lab3w_fr/admin/tmp/';
006    
007    $website = $result['config']['value'];
008    
009    $file_year = $result['config']['value'].'-'.$year;
010
011    //echo '<br/>Le fichier <code>'.$directoryTmp.'awstats_'.$file_year.'.php</code>';
012    //if(!file_exists($directoryTmp.'awstats_'.$file_year.'.php')) 
013    {
014        
015    $write = '<?php'."\n";
016    
017    $write.= '/********************************************************************'."\n";
018    $write.= ' *'."\n";
019    $write.= ' * WebSite : '.$website.''."\n";
020    $write.= ' * Description : Resume des Statistiques Annuelle (AWStats)'."\n";
021    $write.= ' * File : awstats_'.$file_year.'.php'."\n";
022    $write.= ' * Date : '.date('Y/m/d H:i:s')."\n";
023    $write.= ' *'."\n";
024    $write.= ' ************************************/'."\n\n";
025    
026    $write.= '// STATS -----------------------------------------------------------'."\n";
027    $write.= '$result[''.$website.''][''.$year.'']['config']['caption'] = ''.$result['config']['caption'].'';'."\n";
028    $write.= '$result[''.$website.''][''.$year.'']['config']['value'] = ''.$result['config']['value'].'';'."\n";
029    
030    $write.= '$result[''.$website.''][''.$year.'']['period']['caption'] = ''.$result['period']['caption'].'';'."\n";
031    $write.= '$result[''.$website.''][''.$year.'']['period']['value'] = ''.$result['period']['value'].'';'."\n";
032    
033    $write.= '$result[''.$website.''][''.$year.'']['UV']['caption'] = ''.$result['UV']['caption'].'';'."\n";
034    $write.= '$result[''.$website.''][''.$year.'']['UV']['value'] = '.var_export($result['UV']['value'],1).';'."\n";
035    $write.= '$result[''.$website.''][''.$year.'']['NV']['caption'] = ''.$result['NV']['caption'].'';'."\n";
036    $write.= '$result[''.$website.''][''.$year.'']['NV']['value'] = '.var_export($result['NV']['value'],1).';'."\n";
037    $write.= '$result[''.$website.''][''.$year.'']['P']['caption'] = ''.$result['P']['caption'].'';'."\n";
038    $write.= '$result[''.$website.''][''.$year.'']['P']['value'] = '.var_export($result['P']['value'],1).';'."\n";
039    $write.= '$result[''.$website.''][''.$year.'']['H']['caption'] = ''.$result['H']['caption'].'';'."\n";
040    $write.= '$result[''.$website.''][''.$year.'']['H']['value'] = '.var_export($result['H']['value'],1).';'."\n";
041    $write.= '$result[''.$website.''][''.$year.'']['B']['caption'] = ''.$result['B']['caption'].'';'."\n";
042    $write.= '$result[''.$website.''][''.$year.'']['B']['value'] = '.var_export($result['B']['value'],1).';'."\n";
043    
044    $write.= '//------------------------------------------------------------------'."\n\n";
045    
046    $write.= "\n".'?>';
047    
048    if( file_exists($directoryTmp.'awstats_'.$file_year.'.php') ) {
049        //echo ' existe ';
050        $fp = @fopen($directoryTmp.'awstats_'.$file_year.'.php', "w+");
051    } else {
052        //echo ' n'existe pas ';
053        //echo ' ---> on le cree (true)';
054        $fp = @fopen($directoryTmp.'awstats_'.$file_year.'.php', "x+");
055    }
056    
057    //echo ' ---> on ecrit';
058    //echo fputs($fp, $write) ? ' (true)<br/>' : ' (false)<br/>';
059    
060    @fputs($fp, $write);
061    
062    @fclose($fp);
063    
064    @chmod($directoryTmp.'awstats_'.$file_year.'.php', octdec("664")); //exit;
065    @chgrp($directoryTmp.'awstats_'.$file_year.'.php', "1000"); //exit;
066    
067    }
068    
069}

La fonction awstats_resume_write écrit tout simplement un fichier dans un répertoire définit, ici /var/pro/web_sites/lab3w_fr/admin/tmp/.

Lecture du fichier en comptabilisant

Script avec 34 lignes


001function awstats_resume_read_file($website) {
002    
003    $directoryTmp = '/var/pro/web_sites/lab3w_fr/admin/tmp/';
004    $dirhandle = opendir($directoryTmp);
005    
006    while(($file = @readdir($dirhandle)) !== false)
007    {
008        if (($file != '.') && ($file != '..') && (substr("$file", 0, 1) != '.'))
009        {
010            $currentfile = $directoryTmp.''.$file;
011            $currentfilefake = '/'.$file;
012            
013            
014            if (filetype($currentfile) != 'dir' && preg_match("/awstats_".$website."/Ui", $currentfile, $match))
015            {
016                include ''.$currentfile;
017                
018                //echo $currentfile.'<br/>';
019            }
020            
021        }
022        
023    }
024
025    asort($result[$website], SORT_REGULAR);
026    krsort($result[$website]);
027    
028    return isset($result) ? $result : null;
029    
030}
031
032# Affichage des stats pour toutes les années d'un site, d'une configuration
033//$awstats = awstats_resume_read_file('www.zw3b.fr');
034//echo '<code>'.print_r($awstats,1).'</code>';

La fonction awstats_resume_read_file, retourne un tableau PHP (array) avec les variables classées par années.


J'ai fais une page AWStats ici, qui peut vous donnez un exemple de ce que cela peut représenter au final :)


Cordialement,
Romain




Liens Web :





Author of the page

O.Romain.Jaillet-ramey

O.Romain.Jaillet-ramey

  • Firstname : Olivier Romain Luc
  • Lastname : : Jaillet-ramey
  • Arrived on tuesday 19 october 1976 (1976/10/19 00:00)
    44 years activity !

Firefox Nighlty

Our friends from Framasoft are interested in Mozilla and asked them questions about Nightly: Firefox Night-club, free entry !






Valid XHTML 1.0 Strict CSS Valide !

ipv6 ready