PHP Classes
elePHPant
Icontem

File: daystats.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Roberto Bertó  >  SimpleDB  >  daystats.php  >  Download  
File: daystats.php
Role: Example script
Content type: text/plain
Description: Stats of everyday with email option
Class: SimpleDB
Store and read arrays from file based databases
Author: By
Last change:
Date: 13 years ago
Size: 1,908 bytes
 

Contents

Class file image Download
<?PHP

/*
Example of a internal dayly stats counter with stats

TEST STEP 1
run the script in the main index.php of the site and, everyday will'll receive an email

TEST STEP 2
-- view the stats
daystats.php?stats=password
*/

    // set to 0 if no sendmail


   
$config[email][send] = 0;
       
// configure this parms
       
$config[email][subject] = "Stats od day #day#";
       
$config[email][body] = "Total: #total#\Today: #today# ";
       
$config[email][to] = "to@server.com";
       
$config[email][from] = '"SimpleDayStats" <simpleday@server.com>';

   
// set to 0 if running under windows
   
$config[lockfiles] = 1;
   
// the password
   
$config[password] = "password";
   
// the file will be save
   
$config[database] = 'daystats.dat';
   
// the SimpleDB
   
include_once ("db.php");

   
$SimpleDB = new SimpleDB;
   
$SimpleDB->filename = $config[database];
   
$SimpleDB->lockfiles = $config[lockfiles];

   
$db = $SimpleDB->db_get();
   
// If the DB open continue

   
$today = date("Y-m-d");


    if (
$db !== FALSE) {
        if (
$stats == $config[password]) {
           
reset($db);
           
ksort($db);
           
#$db = array_reverse($db);
           
print "<table border=1>";
            while (list(
$a,$b) = each($db)) {
                print
"<tr><td>$a</td><td>$b</td></tr>\n";
            }
            print
"</table>";
        }
        else {
       
//increse counter
           
if ($db[today] != $today & $config[email][send] == 1) {
               
$s = array("/\#day\#/i","/\#total\#/i","/\#today\#/i");
               
$yesterday = $db[today];
               
$r = array($today,$db[total],$db[$yesterday]);
               
$config[email][subject] = preg_replace($s,$r,$config[email][subject]);
               
$config[email][body] = preg_replace($s,$r,$config[email][body]);
               
mail($config[email][to],$config[email][subject],$config[email][body],"From: $config[email][from]");
            }
           
$db[$today]++;
           
$db[today] = $today;
           
$db[total]++;
           
//write db
           
$write = $SimpleDB->db_write($db);
       
// the stats part
       
}
    }
?>