Statistiques de fréquentation d'un site

  Information

Afficher les statistiques de fréquentation du jour et de l'année souhaitée.

C'est la fonction stat () qui ajoute les statistiques dans la table SQL "stat". c'est l'apel de cette fonction que vous devez placer dans le head.php de votre site. Comme ceci : stat();

Ce script compte les visites sur votre site (1 ip / jour). On peut afficher ensuite les résultats sur une période donnée (par jour, mois ou années)

  code source classé dans  Statistiques

 
 01    
 02    
 03    
 04    
 05    
 06    
 07    
 08    
 09    
 10    
 11    
 12    
 13    
 14    
 15    
 16    
 17    
 18    
 19    
 20    
 21    
 22    
 23    
 24    
 25    
 26    
 27    
 28    
 29    
 30    
 31    
 32    
 33    
 34    
 35    
 36    
 37    
 38    
 39    
 40    
 41    
 42    
 43    
 44    
 45    
 46    
 47    
 48    
 49    
 50    
 51    
 52    
 53    
 54    
 55    
 56    
 57    
 58    
 59    
 60    
 61    
 62    
 63    
 64    
 65    
 66    
 67    
 68    
 69    
 70    
 71    
 72    
 73    
 74    
 75    
 76    
 77    
 78    
 79    
 80    
 81    
 82    
 83    
 84    
 85    
 86    
 87    
 88    
 89    
 90    
 91    
 92    
 93    
 94    
 95    
 96    
 97    
 98    
 99    
 100    
 101    
 102    
 103    
 104    
 105    
 106    
 107    
 108    
 109    
 110    
 111    
 112    
 113    
 114    
 115    
 116    
 117    
 118    
 119    
 120    
 121    
 122    
 123    
 124    
 125    
 126    
 127    
 128    
 129    
 130    
 131    
 132    
 133    
 134    
 135    
 136    
 137    
 138    
 139    
 140    
 141    
 142    
 143    
 144    
 145    
 146    
 147    
 148    
 149    
 150    
 151    
 152    
 153    
 154    
 155    
 156    
 157    
 158    
 159    
 160    
 161    
 162    
 163    
 164    
 165    
 166    
 167    
 168    
 169    
 170    
 171    
 172    
 173    
 174    
 175    
 176    
 177    
 178    
 179    
 180    
 181    
 182    
 183    
 184    
 185    
 186    
 187    
 188    
 189    
 190    
 191    
 192    
 193    
 194    
 195    
 196    
 197    
 198    
 199    
 200    
 201    
 202    
 203    
 204    
 205    
 206    
 207    
 208    
 209    
 210    
 211    
 212    
 213    
 214    
 215    
 216    
 217    
 218    
 219    
 220    
 221    
 222    
 223    
 224    
 225    
 226    
 227    
 228    
 229    
 230    
 231    
 232    
 233    
 234    
 235    
 236    
 237    
 238    
 239    
 240    
 241    
 242    
 243    
 244    
 245    
 246    
 247    
 248    
 249    
 250    
 251    
 252    
 253    
 254    
 255    
 256    
 257    
 258    
 259    
 260    
 261    
 262    
 263    
 264    
 265    
 266    
 267    
 268    
 269    
 270    
 271    
 272    
 273    
 274    
 275    
 276    
 277    
 278    
 279    
 280    
 281    
 282    
 283    
 284    
 285    
 286    
 287    
 288    
 289    
 290    
 291    
 292    
 293    
 294    
 295    
 296    
 297    
 298    
 299    
 300    
 301    
 302    
 303    
 304    
 305    
 306    
 307    
 308    
 309    
 310    
 311    
 312    
 313    
 314    
 315    
 316    
 317    
 318    
 319    
 320    
 321    
 322    
 323    
 324    
 325    
 326    
 327    
 328    
 329    
 330    
 331    
 332    
 333    
 334    
 335    
 336    
 337    
 338    
 339    
 340    
 341    
 342    
 343    
                               
<?php
/*---------------------------------------------------------------*/
/*
    Titre : Statistiques de fréquentation d'un site                                                                      
                                                                                                                          
    URL   : https://phpsources.net/code_s.php?id=71
    Auteur           : R@f                                                                                                
    Date édition     : 03 Oct 2004                                                                                        
    Date mise à jour : 19 Sept 2019                                                                                      
    Rapport de la maj:                                                                                                    
    - fonctionnement du code vérifié                                                                                    
*/
/*---------------------------------------------------------------*/?>
CREATE TABLE `stats` (
  `ip` varchar( 18 ) NOT NULL default '',
  `date_visite` date NOT NULL default '0000-00-00'
) TYPE=MyISAM;


*****************
Fichier config.inc.php
*****************
<?php
if(!defined('__CONFIG_INC_PHP__'))
{
  define('__CONFIG_INC_PHP__'1);

  define ('MYSQL_HOST''localhost');
  define ('MYSQL_USER''user');
  define ('MYSQL_PASS''pass');
  define ('MYSQL_BASE''base'); // base de données à utiliser
  define ('MYSQL_TABLE_STATS''stats'); // table des stats
  
  define ('COULEUR_TAB_1''#EFEFEF'); // couleur 1 du tableau
  define ('COULEUR_TAB_2''#DEE3E7'); // couleur 2 du tableau
  
  define ('MSG_JOUR''Journée du'); // Période (pour l'affichage journalier)
  define ('MSG_MOIS''Mois de'); // Période (pour l'affichage mensuel)
  define ('MSG_AN''Année'); // Période (pour l'affichage annuel)
  define ('DATE_SEPARATEUR''/'); 
// Séparateur entre jours & mois et mois & an
  
  $MOIS array('Janvier''Février''Mars''Avril''Mai''Juin''Juillet'
,
 'Août''Septembre',
 'Octobre''Novembre''Décembre');
}
?>

*************
Fichier funcs.php
*************
<?php
if(!defined('__FUNCS_PHP__'))
{
  define ('__FUNCS_PHP__'1);
  
  // teste la validité d'une date
  function date_valide($date)
  {
    // si jour < 1 || $jour > 31
    if($date[0] < || $date[0] > 31)
      return false;
      
    // si mois < 1 || $mois > 12
    if($date[1] < || $date[1] > 12)
      return false;
      
    // si mois à 30 jour && jour == 31
    if(($date[1] == || $date[1] == || $date[1] == || $date[1] == 11) && 
$date[0] == 31)
        return false;
    // si mois == février && jour > 28  
    else if($date[1] == && $date[0] > 28)
    {
      // si jour > 29
      if($date[0] > 29)
        return false;
        
      // si !année bissextile
      if($date[2] % 100 == || $date[2] % != 0)
        return false;
    }
        
    return true;    
  }
  
  // passe une date de format quelconque en date de format jjmmaaa
  function post_dates(&$from, &$to)
  {
    // jours
    if(empty($from[0]))
      $from[0] = '00';
    if(empty($to[0]))
      $to[0] = '00';
      
    if(strlen($from[0]) == 1)
      $from[0] = '0' $from[0];
      
    if(strlen($to[0]) == 1)
      $to[0] = '0' $to[0];
      
    // mois
    if(empty($from[1]))
      $from[1] = '00';
    if(empty($to[1]))
      $to[1] = '00';
      
    if(strlen($from[1]) == 1)
      $from[1] = '0' $from[1];
      
    if(strlen($to[1]) == 1)
      $to[1] = '0' $to[1];
      
    // ans
    $len strlen($from[2]);
    if($len 4)
    {
      if($len == 2)
        $from[2] = $from[2] . '00';
      if($len == 3)
        $from[2] = $from[2] . '0';
    }
    
    $len strlen($to[2]);
    if($len 4)
    {
      if($len == 2)
        $to[2] = $to[2] . '00';
      if($len == 3)
        $to[2] = $to[2] . '0';
    }
      
    $from $from[0] . $from[1] . $from[2];
    $to $to[0] . $to[1] . $to[2];
  }
  
  // fonction qui rempli la table des stats
  function stats()
  {
    include 'config.inc.php';

     $conn mysqli_connect(MYSQL_HOST,MYSQL_USERMYSQL_PASSMYSQL_BASE);


      
    // rcéupération des infos
    $ip $_SERVER['REMOTE_ADDR'];
    $date date('Y-n-j');
    
    // deja un enregistrement pour cette ip et ce jour?
    $result $conn->query("SELECT ip FROM " MYSQL_TABLE_STATS .
" WHERE date_visite='$date' AND ip='$ip'");
    if(!$result)
      return 0;
    
    if($result->num_rows == 0)
    {
      $conn->query("INSERT INTO " MYSQL_TABLE_STATS .
" VALUES ('$ip', '$date')"))

    }
    
    return 1;
  }
}
?>

*************
Fichier index.php
*************
<?php
include 'config.inc.php';
include 'funcs.php';

// affichage du formulaire
if(!isset($_POST['aff']))
{
  stats();
  ?>
  <style type="text/css">
  input {font-size: 13px;}
  div, span {font-size: 13px;}
  </style>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
  <br>
  <?php
  if(isset($_GET['from']) || isset($_GET['to']))
    echo 
'<span style="color: #FF0000;">Une des date que vous avez entré n\'est pas' .
' valide</span><br><br>';
  ?>
  
  <div style="position: relative; left: 20px;"> Du:  
    <input type="text" name="from[0]" maxlength="2" size="2" value="<?php if(
isset($_GET['from'])) echo substr($_GET['from'], 02); ?>">
      /  
    <input type="text" name="from[1]" maxlength="2" size="2" value="<?php if(
isset($_GET['from'])) echo substr($_GET['from'], 22); ?>">
      /  
    <input type="text" name="from[2]" maxlength="4" size="3" value="<?php if(
isset($_GET['from'])) echo substr($_GET['from'], 44); ?>">
       au   
    <input type="text" name="to[0]" maxlength="2" size="2" value="<?php if(isset

($_GET['to'])) echo substr($_GET['to'], 02); ?>">
      
    <input type="text" name="to[1]" maxlength="2" size="2" value="<?php if(isset

($_GET['to'])) echo substr($_GET['to'], 22); ?>">
      
    <input type="text" name="to[2]" maxlength="4" size="3" value="<?php if(isset

($_GET['to'])) echo substr($_GET['to'], 44); ?>">
      <br>
    <br>
    <input type="radio" name="aff" value="jour" checked>
    Affichage journalier<br>
    <input type="radio" name="aff" value="mois">
    Affichage mensuel<br>
    <input type="radio" name="aff" value="an">
    Affichage annuel<br>
  </div>
  
  <div style="position: relative; left: 50px;">
    <br><input type="submit" value="Voir les statistiques">
  </div>
  </form>
  <?php
}
else
{
  $aff $_POST['aff'];
  $from $_POST['from'];
  $to $_POST['to'];
  
  // si année : 04 --> 2004
  if(strlen($from[2]) < 3)
    $from[2] = '20' $from[2];
    
  // si année : 04 --> 2004
  if(strlen($to[2]) < 3)
      $to[2] = '20' $to[2];
  
  // si une date n'est pas valide
  if(!date_valide($from) || !date_valide($to))
  {
    post_dates($from$to);
    header("location: " $_SERVER['PHP_SELF'] . '?from=' $from '&to=' $to

);
    exit;
  }
  
$conn mysqli_connect(MYSQL_HOST,MYSQL_USERMYSQL_PASSMYSQL_BASE);
  
  if($aff == 'jour')
    $sql_query "SELECT date_visite, count(*) FROM " MYSQL_TABLE_STATS 
" WHERE date_visite >= '$from[2]-$from[1]-$from[0]' AND date_visite <" .
" '$to[2]-$to[1]-$to[0]' GROUP BY date_visite ORDER BY date_visite ASC";
  else if($aff == 'mois')
    $sql_query "SELECT date_visite, count(*) FROM " MYSQL_TABLE_STATS 
" WHERE date_visite >= '$from[2]-$from[1]-$from[0]' AND date_visite <" .
" '$to[2]-$to[1]-$to[0]' GROUP BY MONTH(date_visite), YEAR(date_visite) ORDER" .
" BY date_visite ASC";
  else if($aff == 'an')
    $sql_query "SELECT YEAR(date_visite), count(*) FROM " MYSQL_TABLE_STATS 


" WHERE date_visite >= '$from[2]-$from[1]-$from[0]' AND date_visite <" .
" '$to[2]-$to[1]-$to[0]' GROUP BY YEAR(date_visite) ORDER BY date_visite ASC";
    
  $result $conn->query($sql_query);
  if(!$result)
  {
    echo 'Erreur lors de la récupération des résultats<br>';
    exit;
  }
  
  if($result->num_rows 0)
  {
    echo 
"\r\n<table style=\"width: 100%;\" cellspacing=\"0\" cellspadding=\"0\">";
    echo 
"\r\n\t<tr><td style=\"width: 75%; background-color: #DEE3E7; padding-left:" .
" 10px;\"><span style=\"font-size: 14px; font-weight: bold;\">Période</span></"
 .
"td><td style=\"width: 25%; background-color: #DEE3E7;\"><span style=\"font-si" 
.
"ze: 14px; font-weight: bold;\">Nombre de visiteurs</span></td>";
  
    $color 0;
    while($ret mysqli_fetch_array($result))
    {
      $background_color $color == COULEUR_TAB_1 COULEUR_TAB_2;
      
      echo "\r\n\t<tr>";
      if($aff == 'jour')
      {
        echo 
"\r\n\t\t<td style=\"background-color: $background_color; padding-left:" .
" 10px;\"><span style=\"font-size: 13px;\">";
        echo MSG_JOUR ' ' .  substr($ret[0], 82) . DATE_SEPARATEUR substr(
$ret[0], 52) . DATE_SEPARATEUR substr($ret[0], 04);
        echo '</span></td>';
        echo 
"\r\n\t\t<td style=\"background-color: $background_color;\"><span" .
" style=\"font-size: 13px;\">" $ret[1] . '</span></td>';
      }
      else if($aff == 'mois')
      {
        echo 
"\r\n\t\t<td style=\"background-color: $background_color; padding-left:" .
" 10px;\"><span style=\"font-size: 13px;\">";
        echo MSG_MOIS ' ' $MOIS[substr($ret[0], 52) - 1] . ' ' substr(
$ret[0], 04);
        echo '</span></td>';
        echo 
"\r\n\t\t<td style=\"background-color: $background_color;\"><span" .
" style=\"font-size: 13px;\">" $ret[1] . '</span></td>';
      }
      else if($aff == 'an')
      {
        echo 
"\r\n\t\t<td style=\"background-color: $background_color; padding-left:" .
" 10px;\"><span style=\"font-size: 13px;\">";
        echo MSG_AN ' ' $ret[0];
        echo '</span></td>';
        echo 
"\r\n\t\t<td style=\"background-color: $background_color;\"><span" .
" style=\"font-size: 13px;\">" $ret[1] . '</span></td>';
      }
      echo "\r\n\t</tr>";
      
      $color $color == 0;
    }
  
    echo "\r\n</table>";
  }
}
?>

          Fonctions du code - Doc officielle PHP

   php.net   Description Versions PHP OUTIL
   array Crée un tableau PHP 4, PHP 5, PHP 7, PHP 8
   date Formate une date/heure locale PHP 4, PHP 5, PHP 7, PHP 8
   define Définit une constante PHP 4, PHP 5, PHP 7, PHP 8
   defined Vérifie l'existence d'une constante PHP 4, PHP 5, PHP 7, PHP 8
   echo Affiche une chaîne de caractères PHP 4, PHP 5, PHP 7, PHP 8
   empty Détermine si une variable contient une valeur non nulle PHP 4, PHP 5, PHP 7, PHP 8
   exit Affiche un message et termine le script courant PHP 4, PHP 5, PHP 7, PHP 8
   header Envoie un en-tête HTTP PHP 4, PHP 5, PHP 7, PHP 8
   isset Détermine si une variable est affectée PHP 4, PHP 5, PHP 7, PHP 8
   mysqli_connect Ouvre une connexion à un serveur MySQL PHP 5, PHP 7, PHP 8
   mysqli_fetch_array Retourne une ligne de résultat sous la forme d'un tableau associatif PHP 5, PHP 7, PHP 8
   return Retourne le controle du programme au module appelant. PHP 4, PHP 5, PHP 7, PHP 8
   strlen Calcule la taille d'une chaîne PHP 4, PHP 5, PHP 7, PHP 8
   substr Retourne un segment de chaîne PHP 4, PHP 5, PHP 7, PHP 8

   Dites merci aux auteurs pour leurs travail, ça ne coûte rien et ça fait toujours plaisir wink

Présentation de PHP

PHP débutant et initié 50 Tutoriel

Présentation de MySQL