[PHP] contatore con sqlite

luigi777

Utente Attivo
14 Feb 2008
1.086
1
38
42
Massa, Italy
Salve, ho trovato questo codice . servirebbe un contatore come questo. ma non capisco perché non funziona.
PHP:
<?php
// logging page hits
$dbfolder = $_SERVER["DOCUMENT_ROOT"]."/data/";
$dbname = $_SERVER["HTTP_HOST"]."_log.sq3";

// check if database file exists first
if(!file_exists($dbfolder.$dbname))
{
 $logdb = new PDO("sqlite:".$dbfolder.$dbname);
 $logdb->exec("CREATE TABLE hits(page VARCHAR(255) PRIMARY KEY, counter INTEGER)");
}
else
{
 $logdb = new PDO("sqlite:".$dbfolder.$dbname);
}

$page = $_SERVER['REQUEST_URI'];

// check if page is already in the hits table
$statement = $logdb->query("SELECT counter FROM hits WHERE page='$page'");
$record = $statement->fetchAll();

// if a record is found
if(sizeof($record) != 0)
{
 $counter = $record[0]['counter']++;
 $logdb->exec("UPDATE hits SET counter=$counter WHERE page='$page'");
 echo "Counter: ".$counter;
}
else
{
 $logdb->exec("INSERT INTO hits(page, counter) VALUES ('$page', 1)");
 echo "Counter: 1";
}

// close connection
$logdb = null;
?>

avete idea?
Grazie mille.
 
Prova questa classetta...
PHP:
/**
* Created by PHPS.
* User: MADR
* Date: 01/03/2019
* Time: 08:25
*/

class GSql {
   public $conn;
   public $counter_current_page=0;
   public function __construct($path='data',$name='contatore') {
      if(file_exists($path.'/'.$name.'.sqlite')){
         $this->conn = new SQLite3($path.'/'.$name.'.sqlite');
      }else{
         mkdir($path,0777,TRUE);
         touch($path.'/'.$name.'.sqlite');
         $this->conn = new SQLite3($path.'/'.$name.'.sqlite');
      }
   }

   /**
    * Logica per azione da eseguire su record
    * @param string $page Pagina se non esiste viene creato un nuovo record
    * @param bool $fire_action Se agire sui dati o far tornare il valore della query
    *
    * @return $this
    */
   public function check_actions($page,$fire_action=true){
      $res=$this->conn->query('SELECT counter FROM hits WHERE page ="'.$page.'"')->fetchArray(SQLITE3_ASSOC);
      if($fire_action)(empty($res))?$this->create_record($page):$this->increment_page($page);
      else $this->counter_current_page=$res;
      return $this;
   }
   public function create_record($page){
      $this->conn->query('INSERT INTO hits (`page`,`counter`) VALUES ("'.$page.'",1)');
      return $this->check_actions($page,false);
   }

   public function increment_page($page){
      $this->conn->query('UPDATE  hits SET counter = `counter`+1 WHERE `page`= "'.$page.'"');
      return $this->check_actions($page,false);
   }

   public function migrations(){
      $table='create table hits
            (
               page varchar(255) not null,
               counter bigint default 0
            );';
      $index='create unique index hits_page_uindex on hits (page);';
      $this->conn->query($table);
      $this->conn->query($index);
      return $this;
   }
}

//per richiamarla
include_once 'GSql.php';
$conn=new GSql('data','test');
//prima iterazione creiamo la tabella con la migrations
$conn->migrations();

//bisogna caricare i dati...
//basta cambiare la path su check_actions(nome pagina)
$res=$conn->check_actions('test5/aksdnasd/zcx')->counter_current_page;
print_r($res);
 
ok, ho fatto io uso xampp perché ora voglio usare xampp.. ora mi da questo:
ì
Warning: SQLite3::query(): table hits already exists in D:\xampp\htdocs\GSql.php on line 52

Warning: SQLite3::query(): index hits_page_uindex already exists in D:\xampp\htdocs\GSql.php on line 53
Array ( [counter] => 2 )
 
Basato su cookies
PHP:
class GSql {
   public $conn;
   public $counter_current_page=0;

   private $cookie_name='my_GSql';

   public $current_cookies='';

   private $store=TRUE;

   public function __construct($path='data',$name='contatore') {
      if(file_exists($path.'/'.$name.'.sqlite')){
         $this->conn = new SQLite3($path.'/'.$name.'.sqlite');
      }else{
         mkdir($path,0777,TRUE);
         touch($path.'/'.$name.'.sqlite');
         $this->conn = new SQLite3($path.'/'.$name.'.sqlite');
      }
   }

   /**
    * Logica per azione da eseguire su record
    * @param string $page Pagina se non esiste viene creato un nuovo record
    * @param bool $fire_action Se agire sui dati o far tornare il valore della query
    *
    * @return $this
    */
   public function check_actions($page,$fire_action=true){
      $this->verify_cookies($page);
      $res=$this->conn->query('SELECT counter FROM hits WHERE page ="'.$page.'"')->fetchArray(SQLITE3_ASSOC);
      if($this->store){
         if($fire_action)(empty($res))?$this->create_record($page):$this->increment_page($page);
         else $this->counter_current_page=$res;
      }else $this->counter_current_page=$res;
      return $this;
   }
   public function create_record($page){
      $this->conn->query('INSERT INTO hits (`page`,`counter`) VALUES ("'.$page.'",1)');
      return $this->check_actions($page,false);
   }

   public function increment_page($page){
      $this->conn->query('UPDATE  hits SET counter = `counter`+1 WHERE `page`= "'.$page.'"');
      return $this->check_actions($page,false);
   }

   public function migrations(){
      $table='create table hits
            (
               page varchar(255) not null,
               counter bigint default 0
            );';
      $index='create unique index hits_page_uindex on hits (page);';
      $this->conn->query($table);
      $this->conn->query($index);
      return $this;
   }


   public function verify_cookies($page){
      if(isset($_COOKIE[$this->cookie_name])){
         $this->current_cookies=$_COOKIE[$this->cookie_name];
         $result=explode(',',$this->current_cookies);
         if(in_array($page,$result))$this->store=FALSE;
         else{
            $result[]=$page;
            $this->store=TRUE;
            $this->current_cookies=join(',',$result);
         }
      }else{
         $this->current_cookies=join(',',[$page]);
         $this->store=TRUE;
      }

      if($this->store){
         setcookie($this->cookie_name,$this->current_cookies);
      }

   }


}
 
Stampa....
PHP:
include_once 'GSql.php';
$conn=new GSql('data','test');

$res=$conn->check_actions('test1')->counter_current_page;

print_r($res);

// Per fare delle prove fai così
$res=$conn->check_actions(join('/',$_GET))->counter_current_page;

print_r($res);
Inteso che la pagina di test sarà del tipo:
host/test.php?page=ciao&c=true&f=gth
 

Discussioni simili