该类实现了php session的基本原理操作
由于session比较简单,直接封装出了session的基本操作
<?php /** * Created by PhpStorm. * User: 10671 * Date: 2018/6/22 * Time: 23:48 */ class SessionHandle { private static $_instance; private $config = array( 'cookie\_name' => 'session\_id', 'cookie_path' => '', 'cookie_domain' => '', 'cookie_secure' => false, 'save\_path' => \_\_DIR__ . DIRECTORY\_SEPARATOR . 'runtime' . DIRECTORY\_SEPARATOR,//保存路径 'save\_path\_num' => 5,//保存目录数(以免造成一个目录太多session文件,0则保存在当前目录) 'cache_expire' => 30 * 60,//过期时间 'session_handler' => null, 'gc_probability' => 1//触发垃圾回收概率1/100 ); public $path_dictionary = array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' ); public function __construct($config = array()) { foreach ($config as $key => $value) { if (isset($this->config\[$key\])) { $this->config\[$key\] = $value; } } } public function __destruct() { $this->saveSession(); } /** * 单例 * @return SessionHandle */ public static function getInstance($config=array()) { if (!(self::$_instance instanceof self)) { self::$_instance = new static($config); //new static和new self区别可查看http://www.jb51.net/article/54167.htm } return self::$_instance; } /** * 开始设置session */ public function startSession($session_id = null) { if (is\_callable($this->config\['session\_handler'\])) { return $this->config\['session\_handler'\]('start\_session'); } if (mt\_rand(1, 100) <= $this->config\['gc\_probability'\]) { $this->gc(); } if (!isset($\_COOKIE\[$this->config\['cookie\_name'\]\])) { $session\_id === null && $session\_id = $this->getSessionId(); $this->setCookie($session_id); $this->saveSessionData($session_id, array()); } else { $session\_id = $\_COOKIE\[$this->config\['cookie_name'\]\]; } return $data = $\_SESSION = $this->getSessionData($session\_id); } /** * 设置 * @param $session_id * @param $name * @param $value */ public function setSession($name, $value, $session_id = null) { $session\_id === null && $session\_id = $this->getSessionId(); $data = $this->getSessionData($session_id); if (empty($name)) { return false; } if ($value == null) { unset($data\[$name\]); } $data\[$name\] = $value; $result = $this->saveSessionData($session_id, $data); if ($result === false) { return $result; } return $_SESSION = $data; } /** * 保存数据到文件 * @param null $session_id * @param null $data * @return bool|int|null */ public function saveSession($session_id=null,$data=null){ $session\_id === null && $session\_id = $this->getSessionId(); $data === null && $data = $_SESSION; if (is\_callable($this->config\['session\_handler'\])) { return $this->config\['session\_handler'\]('save\_session', $session_id); } $result = $this->saveSessionData($session_id, $data); if ($result === false) { return $result; } return $_SESSION = $data; } /** * 删除 * @param null $session_id * @return bool */ public function deleteSession($session_id = null) { $session\_id === null && $session\_id = $this->getSessionId(); $result = $this->deleteSessionData($session_id); $this->setCookie(''); $_SESSION = array(); return $result; } /** * 设置cookie * @param $session_id */ public function setCookie($session_id) { setcookie($this->config\['cookie\_name'\], $session\_id, time() + $this->config\['cache\_expire'\], $this->config\['cookie\_path'\], $this->config\['cookie\_domain'\], $this->config\['cookie\_secure'\]); } /** * 获取session_id * @param $session_id * @return mixed */ public function getSessionData($session_id) { if (is\_callable($this->config\['session\_handler'\])) { return $this->config\['session\_handler'\]('read', $session\_id); } $this->updateSessionIndex($session_id); $data = serialize(array()); if (file\_exists($this->getSessionIDPath($session\_id))) { $data = file\_get\_contents($this->getSessionIDPath($session_id)); } return unserialize($data); } /** * 修改保存 * @param $session_id * @param $data * @return bool|int */ public function saveSessionData($session_id, $data) { if (is\_callable($this->config\['session\_handler'\])) { return $this->config\['session\_handler'\]('save', $session\_id, $data); } $this->updateSessionIndex($session_id); $result = file\_put\_contents($this->getSessionIDPath($session_id), serialize($data)); return $result; } /** * 删除文件 * @param $session_id * @return bool */ public function deleteSessionData($session_id) { if (is\_callable($this->config\['session\_handler'\])) { return $this->config\['session\_handler'\]('delete', $session\_id); } return unlink($this->getSessionIDPath($session_id)); } /** * 设置session处理 * @param callable $function */ public function setSessionHandler(callable $function) { $this->config\['session_handler'\] = $function; } /** * 获取sessionid的路径 * @param $session_id * @return string */ public function getSessionIDPath($session_id) { if (abs($this->config\['save\_path\_num'\]) == 0) { $path = $this->config\['save_path'\]; } else { $path = $this->config\['save\_path'\] . $this->path\_dictionary\[$this->time33($session\_id) % $this->config\['save\_path\_num'\]\] . DIRECTORY\_SEPARATOR; } if (!is_dir($path)) { @mkdir($path); } return $path . $session_id; } /** * 垃圾回收 * @return bool|int */ public function gc() { if (is\_callable($this->config\['session\_handler'\])) { return $this->config\['session_handler'\]('gc'); } if ($this->config\['cache_expire'\] <= 0) { return true; } $session\_index\_path = $this->config\['save_path'\] . 'index'; $session\_index = unserialize(file\_get\_contents($session\_index_path)); foreach ($session\_index as $session\_id => $value) { if ($value + $this->config\['cache_expire'\] < time()) { unset($session\_index\[$session\_id\]); @unlink($this->deleteSessionData($session_id)); } } $result = file\_put\_contents($session\_index\_path, serialize($session_index)); return $result; } /** * 更新session索引 * @param null $session_id */ public function updateSessionIndex($session_id = null) { if (is\_callable($this->config\['session\_handler'\])) { return $this->config\['session\_handler'\]('update\_session\_index', $session\_id); } if ($this->config\['cache_expire'\] <= 0) { return true; } $session\_id === null && $session\_id = $this->getSessionId(); $session\_index\_path = $this->config\['save_path'\] . 'index'; $session_index = array(); if (file\_exists($session\_index_path)) { $session\_index = unserialize(file\_get\_contents($session\_index_path)); } $session\_index\[$session\_id\] = time(); $result = file\_put\_contents($session\_index\_path, serialize($session_index)); return $result; } /** * 生成一个尽量唯一的字符串 */ public function getSessionId() { $session_id = null; if (isset($\_COOKIE\[$this->config\['cookie\_name'\]\])) { $session\_id = $\_COOKIE\[$this->config\['cookie_name'\]\]; } if (empty($session_id)) { if (is\_callable($this->config\['session\_handler'\])) { return $this->config\['session\_handler'\]('create\_session_id'); } $key = uniqid(mt_rand(100000, 999999)); $session_id = $this->md5($key); } return $session_id; } public function md5($str) { $hash = md5($str); return $hash; } public function time33($str) { $hash = 0; $s = md5($str); $len = 32; for ($i = 0; $i < $len; $i++) { $hash = ($hash * 33 + ord($s{$i})) & 0x7FFFFFFF; } return $hash; } }
该类实现了php基本的session原理
调用方法:
include 'SessionHandle.php'; $session_handle = SessionHandle::getInstance(); //var\_dump($session\_handle); $session_handle->startSession(); //$session_handle->deleteSession(); //$session_handle->setSession('name','ncl'); var\_dump($\_SESSION);