Authored by ziy

添加文件session

... ... @@ -29,12 +29,10 @@ class FileCache extends Root implements CacheInterface
$server = $this->getServerHost('cache');
$this->node = ($node == null ? 'cache' : $node);
$_pathList = $cachePath = $server->getServerConfig($this->section, $this->node);
if (empty($_pathList)) {
if ($childNode == null && is_array($cachePath)) {
$cachePath = (sys_get_temp_dir() . 'cached' . DIRECTORY_SEPARATOR);
} elseif ($childNode != null) {
} else {
$cachePath = $_pathList[$childNode];
} elseif (is_array($cachePath)) {
throw new \Exception('Cache Path is Array.');
}
}
$this->initFileInfo($cachePath);
... ...
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 14/12/25
* Time: 下午4:47
*/
namespace Hood\Core\Session;
use Hood\Core\Support\Str;
use Hood\Core\Root;
use Hood\Cache;
class FileSession extends Root implements SessionHandlerInterface
{
/**
* session 前缀
* @var string
*/
private $sessionPrefix = 'session:';
/**
* Session数据就自动删除时间
* @var int
*/
private $gcMaxLifeTime = 1800;
private $sessionOptions = array(
'use_cookies',
'auto_start',
'serialize_handler',
'cookie_lifetime',
'cookie_path',
'cookie_domain',
'use_cookies',
'cache_expire',
'gc_maxlifetime'
);
private $fileCache;
/**
* 初始化
*/
public function __construct()
{
session_set_save_handler(
array(&$this, "open"),
array(&$this, "close"),
array(&$this, "read"),
array(&$this, "write"),
array(&$this, "destroy"),
array(&$this, "gc")
);
if (version_compare(phpversion(), '5.4.0', '>=')) {
register_shutdown_function('session_write_close');
}
ini_set("session.use_cookies", 1);
ini_set("session.cookie_domain", ".yohobuy.com");
ini_set('session.cookie_path', '/');
ini_set('session.gc_maxlifetime', $this->gcMaxLifeTime);
}
/**
* @return fileCache
*/
private function cache()
{
if (empty($this->fileCache)) {
$this->fileCache = Cache::File();
}
return $this->fileCache;
}
/**
* 设置session参数
* @param array $options
* @return $this
*/
public function setSessionOptions(array $options)
{
foreach ($options as $key => $val) {
if (isset($this->sessionOptions[$key])) {
ini_set('session.' . $key, $val);
}
}
return $this;
}
/**
* 打开Session
* @param string $savePath
* @param string $sessionID
* @return bool
*/
public function open($savePath, $sessionID)
{
return true;
}
/**
* 关闭Session
* @return bool
*/
public function close()
{
return true;
}
/**
* 读取缓存
* @param string $sessionID
* @return mixed|string
*/
public function read($sessionID)
{
return $this->cache()->get($this->sessionPrefix . $sessionID);
}
/**
* 写入Session
* @param string $sessionID
* @param string $sessionData
* @return bool
*/
public function write($sessionID, $sessionData)
{
return $this->cache()->set($this->sessionPrefix . $sessionID, $sessionData, $this->gcMaxLifeTime);
}
/**
* 注销Session
* @param int $sessionID
* @return bool
*/
public function destroy($sessionID)
{
return $this->cache()->delete($this->sessionPrefix . $sessionID);
}
public function gc($maxlifetime)
{
return true;
}
public function __destruct()
{
session_write_close();
}
public function generateSessionId()
{
return sha1(uniqid('', true) . Str::random(25) . microtime(true));
}
}
\ No newline at end of file
... ...
... ... @@ -10,6 +10,7 @@ namespace Hood;
use Hood\Core\Session\SessionNamespace;
use Hood\Core\Session\CacheSession;
use Hood\Core\Session\FileSession;
class Session
{
... ... @@ -23,7 +24,6 @@ class Session
*/
static public function start($namespace = 'session_default')
{
if (self::isSessionStarted() == false || self::$_session == null) {
self::$_session = new CacheSession();
session_start();
... ... @@ -32,6 +32,21 @@ class Session
}
/**
* 开启Session并设置NameSpace,使用File
* @param $namespace
* @return SessionNamespace
*/
static public function fileStart($namespace = 'session_default')
{
if (self::isSessionStarted() == false || self::$_session == null) {
self::$_session = new FileSession();
session_start();
}
return new SessionNamespace ($namespace);
}
/**
* 检查Session是否开启
* @return bool
*/
... ...