Ricontrollandoil codice, ho notato qualche errorino che ho corretto:
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) {
return $this->existsValue($child);
}
public function setValue($child,$value) {
if ($this->existsValue($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->existsValue($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) {
if (eregi('<'.$child.'>(.+)</'.$child.'>',$this->xmlContent,$args)) {
return $args[0];
}
else {
return false;
}
}
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);
}
}
?>


Rispondi quotando