Ciao a tutti ho iniziato da poco ad addentrarmi nel mondo del PHP 5 e del lato OO di questo linguaggio...
Ho scritto una classe per la gestione delle sessioni su db e mi piacerebbe avere dei pareri!!
La classe presume la creazione di 1 db con 2 tabelle:
Tabella sessioni:
id || uid || user_id || creation_date
id chiave primaria auto increment
Tabella utenti:
id || user || password || altri campi a scelta ||....||
id chiave primaria auto increment
Ora veniamo alla classe:
versione compatibile php4-5
Codice PHP:
<?php
ob_start();
class db_sess{
//MEMBRI PRIVATI NON MODIFICARE
var $uid;
var $conn;
var $error;
//$expire regola la durata della sessione, il suo valore è espresso in secondi,
//aumentare il valore della variabile per allungare la vita della ssessione e viceversa
var $expire = 3600;
//COSTRUTTORE
function db_sess($h, $u, $p, $d)
{
$arg = func_num_args();
switch($arg)
{
case '4':
if(!$this->conn = @mysql_connect($h,$u,$p))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
if(!@mysql_select_db($d,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
break;
default:
$this->error = "Errore nell'istanziamento della classe
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
}
###############################
########METODI PRIVATI#########
###############################
//OTTIENE L'UID CORRENTE
function get_id()
{
if(isset($_COOKIE['uid'])) $this->uid = $_COOKIE['uid'];
else $this->uid = NULL;
}
//VISUALIZZA EVENTUALI ERRORI
function show_error()
{
echo $this->error;
}
//GENERA UN NUOVO UID
function generate_id()
{
list($usec, $sec) = explode(' ', microtime());
mt_srand((float) $sec + ((float) $usec * 100000));
$this->uid = md5(uniqid(mt_rand(), true));
}
//FUNZIONE DI GARBAGE
function garbage()
{
if($this->uid)
{
$sql = "SELECT * FROM `sessioni` WHERE uid='".$this->uid."'";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
if($ris && @mysql_affected_rows($this->conn) == 1)
{
$riga = mysql_fetch_array($ris);
$now = mktime();
if($now > $riga['creation_date'] + $this->expire)
{
$this->unset_cookie();
}
}
}
$sql = "DELETE FROM`sessioni` WHERE creation_date + ".$this->expire." <= ".time();
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
}
//ELIMINA IL COOKIE DI SESSIONE
function unset_cookie()
{
setcookie("uid","",mktime() - 86400);
}
###############################
########METODI PUBBLICI########
###############################
//CONTROLLA LO STATUS DI SESSIONE
function status()
{
$this->get_id();
$this->garbage();
if(!is_null($this->uid))
{
$sql = "SELECT * FROM `utenti` INNER JOIN `sessioni` ON `utenti`.id = `sessioni`.user_id";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
if(@mysql_affected_rows($this->conn) != 1)
{
return false;
}
return $riga = @mysql_fetch_array($ris);
}
}
//ALLUNGA LA VITA DI UNA SESSIONE
function update()
{
$this->get_id();
$sql = "UPDATE `sessioni` SET `creation_date` = ".time()." WHERE uid='".$this->uid."'";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
}
//REGISTRA UNA SESSIONE
function rec_sess($id)
{
$this->generate_id();
$sql = "INSERT INTO `sessioni`(`uid`,`user_id`,`creation_date`) VALUES ('".$this->uid."','".$id."','".time()."')";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error .= "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
$n = @mysql_insert_id($this->conn);
if(!$n)
{
$this->error = "Impossibile registrare la sessione... riprovare in seguito
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
setcookie("uid",$this->uid,time()+$this->expire);
}
//Distrugge la sessione
function destroy()
{
$this->get_id();
$sql = "DELETE FROM `sessioni` WHERE uid='".$this->uid."'";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
$this->unset_cookie();
}
}
?>
versione compatibile php5
Codice PHP:
<?php
ob_start();
class db_sess{
//MEMBRI PRIVATI NON MODIFICARE
private $uid;
private $conn;
private $error;
//$expire regola la durata della sessione, il suo valore è espresso in secondi,
//aumentare il valore della variabile per allungare la vita della ssessione e viceversa
private $expire = 3600;
//COSTRUTTORE
public function __construct($h="", $u="", $p="", $d="")
{
$arg = func_num_args();
switch($arg)
{
case '4':
if(!$this->conn = @mysql_connect($h,$u,$p))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
if(!@mysql_select_db($d,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
break;
default:
$this->error = "Errore nell'istanziamento della classe
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
}
//DISTRUTTORE
public function __destruct()
{
if(!is_null($this->conn))
{
if(!@mysql_close($this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
$this->uid = NULL;
$this->conn = NULL;
$this->error = NULL;
exit;
}
}
$this->uid = NULL;
$this->conn = NULL;
$this->error = NULL;
}
###############################
########METODI PRIVATI#########
###############################
//OTTIENE L'UID CORRENTE
private function get_id()
{
if(isset($_COOKIE['uid'])) $this->uid = $_COOKIE['uid'];
else $this->uid = NULL;
}
//VISUALIZZA EVENTUALI ERRORI
private function show_error()
{
echo $this->error;
}
//GENERA UN NUOVO UID
private function generate_id()
{
list($usec, $sec) = explode(' ', microtime());
mt_srand((float) $sec + ((float) $usec * 100000));
$this->uid = md5(uniqid(mt_rand(), true));
}
//FUNZIONE DI GARBAGE
private function garbage()
{
if($this->uid)
{
$sql = "SELECT * FROM `sessioni` WHERE uid='".$this->uid."'";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
if($ris && @mysql_affected_rows($this->conn) == 1)
{
$riga = mysql_fetch_array($ris);
$now = mktime();
if($now > $riga['creation_date'] + $this->expire)
{
$this->unset_cookie();
}
}
}
$sql = "DELETE FROM`sessioni` WHERE creation_date + ".$this->expire." <= ".time();
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
}
//ELIMINA IL COOKIE DI SESSIONE
private function unset_cookie()
{
setcookie("uid","",mktime() - 86400);
}
###############################
########METODI PUBBLICI########
###############################
//CONTROLLA LO STATUS DI SESSIONE
public function status()
{
$this->get_id();
$this->garbage();
if(!is_null($this->uid))
{
$sql = "SELECT * FROM `utenti` INNER JOIN `sessioni` ON `utenti`.id = `sessioni`.user_id";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
if(@mysql_affected_rows($this->conn) != 1)
{
return false;
}
return $riga = @mysql_fetch_array($ris);
}
}
//ALLUNGA LA VITA DI UNA SESSIONE
public function update()
{
$this->get_id();
$sql = "UPDATE `sessioni` SET `creation_date` = ".time()." WHERE uid='".$this->uid."'";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
}
//REGISTRA UNA SESSIONE
public function rec_sess($id)
{
$this->generate_id();
$sql = "INSERT INTO `sessioni`(`uid`,`user_id`,`creation_date`) VALUES ('".$this->uid."','".$id."','".time()."')";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error .= "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
$n = @mysql_insert_id($this->conn);
if(!$n)
{
$this->error = "Impossibile registrare la sessione... riprovare in seguito
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
setcookie("uid",$this->uid,time()+$this->expire);
}
//Distrugge la sessione
public function destroy()
{
$this->get_id();
$sql = "DELETE FROM `sessioni` WHERE uid='".$this->uid."'";
if(!$ris = @mysql_query($sql,$this->conn))
{
$this->error = "Errore numero: " . mysql_errno() . "
";
$this->error .= "Descrizione errore: ". mysql_error() . "
";
$this->error .= "Ci scusiamo per l'inconveniente";
$this->show_error();
exit;
}
$this->unset_cookie();
}
}
?>
Ora sbizzarritevi con consigli critiche e tutto ciò che vi viene in mente!!
(NB non ho testato tutto al 100% se qualcosa non funziona fatemelo sapere)