使用session_start参数自定义会话
如何做...
namespace Application\Security;
use ReflectionClass;
use InvalidArgumentsException;
class SessOptions
{
const ERROR_PARAMS = 'ERROR: invalid session options';
如何运行...


更多...
最后更新于
namespace Application\Security;
use ReflectionClass;
use InvalidArgumentsException;
class SessOptions
{
const ERROR_PARAMS = 'ERROR: invalid session options';


最后更新于
const SESS_OP_NAME = 'name';
const SESS_OP_LAZY_WRITE = 'lazy_write'; // AVAILABLE // SINCE PHP 7.0.0.
const SESS_OP_SAVE_PATH = 'save_path';
const SESS_OP_SAVE_HANDLER = 'save_handler';
// etc.protected $options;
protected $allowed;
public function __construct(array $options)
{
$reflect = new ReflectionClass(get_class($this));
$this->allowed = $reflect->getConstants();
$this->allowed = array_flip($this->allowed);
unset($this->allowed[self::ERROR_PARAMS]);
foreach ($options as $key => $value) {
if(!isset($this->allowed[$key])) {
error_log(__METHOD__ . ':' . self::ERROR_PARAMS);
throw new InvalidArgumentsException(
self::ERROR_PARAMS);
}
}
$this->options = $options;
}public function getAllowed()
{
return $this->allowed;
}
public function start()
{
session_start($this->options);
}<?php
require __DIR__ . '/../Application/Autoload/Loader.php';
Application\Autoload\Loader::init(__DIR__ . '/..');
use Application\Security\SessOptions;$options = [
SessOptions::SESS_OP_USE_ONLY_COOKIES => 1,
SessOptions::SESS_OP_COOKIE_LIFETIME => 300,
SessOptions::SESS_OP_COOKIE_HTTPONLY => 1,
SessOptions::SESS_OP_NAME => 'UNLIKELYSOURCE',
SessOptions::SESS_OP_SAVE_PATH => __DIR__ . '/session'
];$sessOpt = new SessOptions($options);
$sessOpt->start();
$_SESSION['test'] = 'TEST';
phpinfo(INFO_VARIABLES);