Stavo pensando da tempo a qualcosa da sviluppare per potermi esercitare con le classi in php.
Mi è venuto in mente di provare a creare un sistema alternativo alle sessioni.
Sviluppando un'applicazione client-server in delphi che utilizzava come protocollo lo scambio di file xml, ho pensato di mettere i valori delle sessioni su un file xml salvato sul server.
Quello che ne è uscito fuori è questo:
Codice PHP:
<?php
class xmlSession {
private $id;
private $xmlContent;
private $file;
public function __construct() {
if (isset($_COOKIE['id'])) {
$this->id = $_COOKIE['id'];
}
else {
$this->id = md5(time());
setcookie('id',$this->id);
}
$this->file = './sessioni/'.$this->id.'.xml';
if (file_exists($this->file)) {
$this->xmlContent = file_get_contents($this->file);
}
else {
$fp = fopen($this->file,'w');
fwrite($fp,'<sessione>'.chr(13).'</sessione>');
fclose($fp);
$this->xmlContent = file_get_contents($this->file);
}
return true;
}
public function getValue($child) {
if ($this->esistsValue($child)) {
return $args[0];
}
else {
return false;
}
}
public function setValue($child,$value) {
if ($this->esistsValue($child)) {
$this->xmlContent =
eregi_replace('<'.$child.'>(.+)</'.$child.'>','<'.$child.'>'.$value.'</'.$child.'>',$this->xmlContent);
}
else {
$this->xmlContent = eregi_replace('<sessione>'.chr(13),'<sessione>'.chr(13).'
<'.$child.'>'.$value.'</'.$child.'>'.chr(13),$this->xmlContent);
}
return true;
}
public function unsetValue($child) {
if ($this->esistsValue($child)) {
$this->xmlContent = eregi_replace('<'.$child.'>(.+)</'.$child.'>'.chr(13),'',$this->xmlContent);
}
$this->saveXml();
return true;
}
public function printXml() {
echo $this->xmlContent;
return true;
}
private function existsValue($child) {
return eregi('<'.$child.'>(.+)</'.$child.'>',$this->xmlContent);
}
private function saveXml() {
$fp = fopen($this->file,'w');
fwrite($fp,$this->xmlContent);
fclose($fp);
return true;
}
public function destroyXml() {
unlink($this->file);
setcookie('id');
unset($this->xmlContent);
}
public function __destruct() {
$this->saveXml();
unset($this->xmlContent);
}
}
?>
Dato che però a casa non ho php 5 ho dovuto sviluppare la classe anche per php 4 da testare on line ed è uscito fuori questo:
Codice PHP:
<?php
class xmlSession {
var $id;
var $xmlContent;
var $file;
function xmlSession() {
if (isset($_COOKIE['id'])) {
$this->id = $_COOKIE['id'];
}
else {
$this->id = md5(time());
setcookie('id',$this->id);
}
$this->file = './sessioni/'.$this->id.'.xml';
if (file_exists($this->file)) {
$this->xmlContent = file_get_contents($this->file);
}
else {
$fp = fopen($this->file,'w');
fwrite($fp,'<sessione>'.chr(13).'</sessione>');
fclose($fp);
$this->xmlContent = file_get_contents($this->file);
}
return true;
}
function getValue($child) {
if ($this->esistsValue($child)) {
return $args[0];
}
else {
return false;
}
}
function setValue($child,$value) {
if ($this->esistsValue($child)) {
$this->xmlContent =
eregi_replace('<'.$child.'>(.+)</'.$child.'>','<'.$child.'>'.$value.'</'.$child.'>',$this->xmlContent);
}
else {
$this->xmlContent = eregi_replace('<sessione>'.chr(13),'<sessione>'.chr(13).'
<'.$child.'>'.$value.'</'.$child.'>'.chr(13),$this->xmlContent);
}
$this->saveXml();
return true;
}
function unsetValue($child) {
if ($this->esistsValue($child)) {
$this->xmlContent = eregi_replace('<'.$child.'>(.+)</'.$child.'>'.chr(13),'',$this->xmlContent);
}
$this->saveXml();
return true;
}
function existsValue($child) {
return eregi('<'.$child.'>(.+)</'.$child.'>',$this->xmlContent);
}
function printXml() {
echo $this->xmlContent;
return true;
}
function saveXml() {
$fp = fopen($this->file,'w');
fwrite($fp,$this->xmlContent);
fclose($fp);
return true;
}
function destroyXml() {
unlink($this->file);
setcookie('id');
unset($this->xmlContent);
}
}
?>
Dato che non sono praticissimo.. c'è qualcosa, a livello logico, di sbagliato in questo sistema?