sid = $string; } public function __construct() { // конструктор класса $this->timeLife = $timeLife; $this->size = $size; self::$gc++; $cookie = $_COOKIE['sid']; if(isset($cookie)) $this->sid = $cookie; else { $this->generate(); setcookie('sid', $this->sid, time()+604800); } if(self::$gc >= 1500) $this->gc(); } public function get($key = 'all') { $fh = fopen($this->savePath . $this->sid, "r"); $data = fgets($fh); fclose($fh); $data = json_decode($data, true); if($key == 'all') return $data; else return $data[$key]; } public function set($arr) { $path = $this->savePath . $this->sid; $fh = fopen($path, "a+"); if(filesize($path) > 0) { $data = fgets($fh); $data = json_decode($data, true); ftruncate($fh, 0); fputs($fh, json_encode(array_merge($data, $arr))); } else fputs($fh, json_encode($arr)); fclose($fh); } public function destroy($id) { unlink($this->savePath . $this.sid); } private function gc() { self::$gc = 0; $dh = opendir($this->savePath); while(($file = readdir($dh)) !== false) { if(time() - filemtime($file) >= $this->timeLife && is_file($this->savePath . $file)) unlink($this->savePath . $file); } } }