Vous êtes ici Codes PHP et MySQL > Statistiques > Statistiques de fréquentation

 

Statistiques de fréquentation

Permet de réaliser des statistiques de fréquentation...

Ce script compte les visites sur votre site (1 ip / jour). On peux afficher ensuite les résultats sur une période donnée (par jour, mois ou années)
 
R@f
Site de l'auteur voir
[17] sources en PHP voir
Code vu 8921 fois
Enregistré le 03 Oct 2004
  • Digg ce code sur digg.com
  • Bookmark ce code sur del.icio.us
  • Bookmark ce code sur Google
  • Bookmark ce code sur Yahoo
  • Ajoute Statistiques de fréquentation
  • Partage ce code sur Facebook
 
 
 
 

Code Source


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.
344.
345.
346.
347.
 
<?php
/******************************************************************************/
/*                                                                            */
/*                       __        ____                                       */
/*                 ___  / /  ___  / __/__  __ _____________ ___               */
/*                / _ \/ _ \/ _ \_\ \/ _ \/ // / __/ __/ -_|_-<               */
/*               / .__/_//_/ .__/___/\___/\_,_/_/  \__/\__/___/               */
/*              /_/       /_/                                                 */
/*                                                                            */
/*                                                                            */
/******************************************************************************/
/*                                                                            */
/* Titre          : Statistiques de fréquentation                             */
/*                                                                            */
/* URL            : http://www.phpsources.org/scripts71-PHP.htm               */
/* Auteur         : R@f                                                       */
/* Date édition   : 03 Oct 2004                                               */
/* Website auteur : http://www.phpsources.org                                 */
/*                                                                            */
/******************************************************************************/
?>
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[] < || $date[] > 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[] == 31)
        return false;
    // si mois == février && jour > 28  
    else if($date[1] == && $date[] > 28)
    {
      // si jour > 29
      if($date[] > 29)
        return false;
        
      // si !année bissextile
      if($date[2] % 100 ==  || $date[2] % != )
        return false;
    }
        
    return true;    
  }
  
  // passe une date de format quelconque en date de format jjmmaaa
  function post_dates(&$from, &$to)
  {
    // jours
    if(empty($from[]))
      $from[] = '00';
    if(empty($to[]))
      $to[] = '00';
      
    if(strlen($from[]) == 1)
      $from[] = '0' $from[];
      
    if(strlen($to[]) == 1)
      $to[] = '0' $to[];
      
    // 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[] . $from[1] . $from[2];
    $to $to[] . $to[1] . $to[2];
  }
  
  // fonction qui rempli la table des stats
  function stats()
  {
    include 'config.inc.php';
    if(!@mysql_connect(MYSQL_HOSTMYSQL_USERMYSQL_PASS) || !@mysql_select_db(
MYSQL_BASE))
      return ;
      
    // rcéupération des infos
    $ip $_SERVER['REMOTE_ADDR'];
    $date date('Y-n-j');
    
    // deja un enregistrement pour cette ip et ce jour?
    $result mysql_query("SELECT ip FROM " MYSQL_TABLE_STATS " WHERE date_visite='$date' AND ip='$ip'");
    if(!$result)
      return ;
    
    if(!mysql_num_rows($result))
    {
      if(!mysql_query("INSERT INTO " MYSQL_TABLE_STATS " VALUES ('$ip', '$date')"))
        return ;
    }
    
    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'], , 2); ?>">
      /  
    <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'], , 2); ?>">
      
    <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;
  }
  
  if(!@mysql_connect(MYSQL_HOSTMYSQL_USERMYSQL_PASS) || !@mysql_select_db(
MYSQL_BASE))
  {
    echo 'Erreur lors de la connectin à la base de données<br>';
    exit;
  }
  
  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 mysql_query($sql_query);
  if(!$result)
  {
    echo 'Erreur lors de la récupération des résultats<br>';
    exit;
  }
  
  if(mysql_num_rows($result) > )
  {
    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 = ;
    while($ret mysql_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[], 82) . DATE_SEPARATEUR substr(
$ret[], 52) . DATE_SEPARATEUR substr($ret[], , 4);
        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[], 52) - 1] . ' ' substr(
$ret[], , 4);
        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[];
        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 ==  ? : ;
    }
  
    echo "\r\n</table>";
  }
}
?>


 

Fonctions du code

: Vérifie l'existence d'une constante - (PHP 4, PHP 5)
: Définit une constante - (PHP 4, PHP 5)
: Crée un tableau - (PHP 4, PHP 5)
: Détermine si une variable contient une valeur non nulle - (PHP 4, PHP 5)
: Calcule la taille d'une chaîne - (PHP 4, PHP 5)
: Ouvre une connexion à un serveur MySQL - (PHP 4, PHP 5, PECL mysql:1.0)
: Sélectionne une base de données MySQL - (PHP 4, PHP 5, PECL mysql:1.0)
: Formate une date/heure locale - (PHP 4, PHP 5)
: Envoie une requête à un serveur MySQL - (PHP 4, PHP 5, PECL mysql:1.0)
: Retourne le nombre de lignes d'un résultat MySQL - (PHP 4, PHP 5, PECL...
: Détermine si une variable est affectée - (PHP 4, PHP 5)
: Affiche une chaîne de caractères - (PHP 4, PHP 5)
: Retourne un segment de chaîne - (PHP 4, PHP 5)
: Envoie un en-tête HTTP - (PHP 4, PHP 5)
: Affiche un message et termine le script courant - (PHP 4, PHP 5)
: - (PHP 4, PHP 5, PECL mysql:1.0)
Ajouter un commentaire
Code de sécurité

Attention: Les champs marqués d'une étoile * sont obligatoires
 
Librairie PHP

Connexion

 
 

Statistiques

 
 

PHP

 
 
 
 
    Offres d'emploi

Plus de 500 offres d'emploi PHP/MySQL

Offres d'emploi
 
    Editeur PHP
 
        Publicité