Skip to content

Instantly share code, notes, and snippets.

@mpratt

mpratt/Session.php

Last active Apr 14, 2016
Embed
What would you like to do?
A wrapper for session management
<?php
/**
* Session.php
*
* @package Bolido
* @author Michael Pratt <pratt@hablarmierda.net>
* @link http://www.michael-pratt.com/
* @license MIT
*/
namespace Session;
/**
* This class wraps the $_SESSION superglobal
*
* @usage
* $session = new Session();
* $session->start();
* $sessions->set('key', 'value');
* if ($session->has('key)){
* echo $session->get('key');
* // value
* }
* $session->close();
*/
class Session
{
/** @var string The name of the session */
protected $name;
/** @var bool Wether or not the session has been started */
protected $started = false;
/** @var array Configuration options */
protected $options = array(
'session.use_trans_sid' => false,
'session.use_cookies' => true,
'session.use_only_cookies' => true,
'session.cookie_httponly' => true,
'session.gc_probability' => '40',
'session.gc_maxlifetime' => 2400,
'arg_separator.output' => '&amp;',
'url_rewriter.tags' => '',
);
/**
* Constructs the session object.
*
* @param string $name The name of the session
* @param array $options
* @param string $url
* @return void
*/
public function __construct($name = 'PHPSESSID', array $options = array(), $url = null)
{
$this->name = strtoupper($name);
$this->options = array_merge($this->options, $options);
foreach ($this->options as $name => $setting) {
@ini_set($name, $setting);
}
// Find the domain of the url for session cookie assignment
if (!empty($url)){
$host = parse_url($url, PHP_URL_HOST);
if (!filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) &&
!filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
{
if (preg_match('~(?:[^\.]+\.)?([^\.]{2,}\..+)\z~i', $host, $domain) == 1)
{
@ini_set('session.cookie_domain', '.' . $domain['1']);
session_set_cookie_params(0, '/', '.' . $domain['1']);
}
}
}
}
/**
* Sets a session variable.
*
* @param mixed $key Session variable name
* @param mixed $value Session variable value
* @return null
*/
public function set($key, $value)
{
$_SESSION[$key] = $value;
}
/**
* Returns a session variable.
*
* @param mixed $key
* @return mixed
*/
public function get($key)
{
if (!$this->has($key)) {
return false;
}
return $_SESSION[$key];
}
/**
* Unsets a Session Key
*
* @param mixed $key
* @return bool
*/
public function delete($key)
{
if ($this->has($key)) {
unset($_SESSION[$key]);
}
}
/**
* Checks whether a session variable exists.
*
* @param mixed $key
* @return bool
*/
public function has($key)
{
return isset($_SESSION[$key]);
}
/**
* Resets all the session values
* @return void
*/
public function reset()
{
$_SESSION = array();
}
/**
* Check whether the session has already been started.
*
* @return bool
*/
public function isStarted()
{
return $this->started;
}
/**
* Starts the sesssion.
*
* @return bool
* @codeCoverageIgnore
*/
public function start()
{
if ($this->started) {
return false;
}
session_name($this->name);
// For Testing Purposes.
if (PHP_SAPI == 'cli')
{
$_SESSION = array();
return $this->started = true;
}
else if (session_start()) {
return $this->started = true;
}
return false;
}
/**
* Sets the session name.
*
* @param string $name Session name
* @return void
*/
public function setName($name)
{
if (!$this->started) {
$this->name = $name;
}
}
/**
* Returns the session name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Returns the sesssion id.
*
* @return mixed False on Error
* @codeCoverageIgnore
*/
public function getId()
{
if (!$this->started) {
return false;
}
return session_id();
}
/**
* Regenerate session id to make session fixation harder.
*
* @param bool $deletePrevious Wether the previous session should be deleted
* @return void False on error
* @codeCoverageIgnore
*/
public function regenerateId($deletePrevious = false)
{
if (!$this->started) {
return false;
}
session_regenerate_id($deletePrevious);
}
/**
* Stores the session data and closes the session
*
* @return null
* @codeCoverageIgnore
*/
public function close()
{
session_write_close();
}
/**
* Destroy the session.
*
* @return bool
* @codeCoverageIgnore
*/
public function destroy()
{
if (!$this->started) {
return false;
}
$this->reset();
session_destroy();
$this->started = false;
return setcookie($this->name, '', time() - 42000);
}
}
?>
<?php
/**
* TestSession.php
*/
use Session\Session;
require 'Session.php';
class TestSession extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$_SESSION = array();
}
public function testSetterGetter()
{
$session = new Session();
$session->setName('CustomName');
$this->assertTrue($session->start());
$this->assertTrue($session->isStarted());
$this->assertEquals($session->getName(), 'CustomName');
$session = new Session();
$this->assertTrue($session->start());
$this->assertTrue($session->isStarted());
$this->assertEquals($session->getName(), 'PHPSESSID');
$session = new Session('PHPSESSID', array(), 'http://www.example.com');
$this->assertTrue($session->start());
$session->set('string_value', 'My Name is Bólido');
$session->set('object_value', (object) array('My Object'));
$session->set('array_value', array('1', '2', '3'));
$this->assertEquals($session->get('string_value'), 'My Name is Bólido');
$this->assertEquals($session->get('object_value'), (object) array('My Object'));
$this->assertEquals($session->get('array_value'), array('1', '2', '3'));
$this->assertTrue($session->has('string_value'));
$this->assertTrue($session->has('object_value'));
$this->assertTrue($session->has('array_value'));
$this->assertFalse($session->has('unknown_key'));
$this->assertFalse($session->has('other_unset_key'));
$this->assertTrue($session->isStarted());
$this->assertEquals($session->getName(), 'PHPSESSID');
$session->reset();
$this->assertFalse($session->has('string_value'));
$this->assertFalse($session->has('object_value'));
$this->assertFalse($session->has('array_value'));
$this->assertFalse($session->has('unknown_key'));
$this->assertFalse($session->has('other_unset_key'));
$session = new Session('PHPSESSID', array(), '192.168.0.1');
$this->assertTrue($session->start());
$session->set('string_value', 'hellow');
$session->set('object_value', (object) array('hi'));
$session->set('array_value', array('1', '2', '3'));
$this->assertEquals($session->get('string_value'), 'hellow');
$this->assertEquals($session->get('object_value'), (object) array('hi'));
$this->assertEquals($session->get('array_value'), array('1', '2', '3'));
$this->assertTrue($session->has('string_value'));
$this->assertTrue($session->has('object_value'));
$this->assertTrue($session->has('array_value'));
$session->delete('string_value');
$session->delete('object_value');
$session->delete('array_value');
$this->assertFalse($session->has('string_value'));
$this->assertFalse($session->has('object_value'));
$this->assertFalse($session->has('array_value'));
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.