模块

Session_Cookie
继承自 Session

基于 Cookie 的 Session 类。

package
BootPHP
category
Session
author
Tinsh
copyright
© 2005-2016 Kilofox Studio

该类在 SYSPATH/classes/session/cookie.php 第 13 行声明。

属性

public static string $default

默认 session 适配器

string(6) "native"

public static array $instances

session 实例

array(0) 

protected array $_data

session 数据

protected bool $_destroyed

session 破坏了吗?

protected bool $_encrypted

加密 session 数据吗?

protected int $_lifetime

cookie 生命周期

protected string $_name

cookie 名称

方法

public __construct( [ array $config = NULL , string $id = NULL ] ) (在 Session 中定义)

Overloads the name, lifetime, && encrypted session settings.

Sessions can only be created using the Session::instance method.

参数

  • array $config = NULL - 配置
  • string $id = NULL - Session Id

Tags

返回值

  • void

源代码

public function __construct(array $config = NULL, $id = NULL)
{
	if (isset($config['name']))
	{
		// 存储会话 ID 的 Cookie 名称
		$this->_name = (string) $config['name'];
	}
	if (isset($config['lifetime']))
	{
		// Cookie 生命周期
		$this->_lifetime = (int) $config['lifetime'];
	}
	if (isset($config['encrypted']))
	{
		if ($config['encrypted'] === true)
		{
			// 使用默认 Encrypt 实例
			$config['encrypted'] = 'default';
		}
		// 开启或关闭数据加密
		$this->_encrypted = $config['encrypted'];
	}
	// 加载会话
	$this->read($id);
}

public __toString( ) (在 Session 中定义)

Session object is rendered to a serialized string. If encryption is enabled, the session will be encrypted. If not, the output string will be encoded using base64_encode.

Tags

返回值

  • string

源代码

public function __toString()
{
	// Serialize the data array
	$data = serialize($this->_data);
	if ($this->_encrypted)
	{
		// Encrypt the data using the default key
		$data = Encrypt::instance($this->_encrypted)->encode($data);
	}
	else
	{
		// Obfuscate the data with base64 encoding
		$data = base64_encode($data);
	}
	return $data;
}

public as_array( ) (在 Session 中定义)

Returns the current session array. The returned array can also be assigned by reference.

// Get a copy of the current session data
$data = $session->as_array();

// Assign by reference for modification
$data =& $session->as_array();

返回值

  • array

源代码

public function & as_array()
{
	return $this->_data;
}

public bind( string $key , mixed & $value ) (在 Session 中定义)

Set a variable by reference.

$session->bind('foo', $foo);

参数

  • string $key required - Variable name
  • byref mixed $value required - Referenced value

返回值

  • $this

源代码

public function bind($key, & $value)
{
	$this->_data[$key] = & $value;
	return $this;
}

public delete( string $key ) (在 Session 中定义)

Removes a variable in the session array.

$session->delete('foo');

参数

  • string $key required - Variable name

返回值

  • $this

源代码

public function delete($key)
{
	$args = func_get_args();
	foreach ($args as $key)
	{
		unset($this->_data[$key]);
	}
	return $this;
}

public destroy( ) (在 Session 中定义)

Completely destroy the current session.

$success = $session->destroy();

返回值

  • boolean

源代码

public function destroy()
{
	if ($this->_destroyed === false)
	{
		if ($this->_destroyed = $this->_destroy())
		{
			// The session has been destroyed, clear all data
			$this->_data = array();
		}
	}
	return $this->_destroyed;
}

public get( string $key [, mixed $default = NULL ] ) (在 Session 中定义)

Get a variable from the session array.

$foo = $session->get('foo');

参数

  • string $key required - Variable name
  • mixed $default = NULL - Default value to return

返回值

  • mixed

源代码

public function get($key, $default = NULL)
{
	return array_key_exists($key, $this->_data) ? $this->_data[$key] : $default;
}

public get_once( string $key [, mixed $default = NULL ] ) (在 Session 中定义)

Get && delete a variable from the session array.

$bar = $session->get_once('bar');

参数

  • string $key required - Variable name
  • mixed $default = NULL - Default value to return

返回值

  • mixed

源代码

public function get_once($key, $default = NULL)
{
	$value = $this->get($key, $default);
	unset($this->_data[$key]);
	return $value;
}

public id( ) (在 Session 中定义)

Get the current session id, if the session supports it.

$id = $session->id();

Not all session types have ids.

返回值

  • string

源代码

public function id()
{
	return NULL;
}

public static instance( [ string $type = NULL , string $id = NULL ] ) (在 Session 中定义)

创建给定类型的 session 单例。 一些会话类型(native、database)支持通过传递一个 session ID 作为第二个参数重新启动一个会话。

$session = Session::instance();

当请求结束时 Session::write() 将被自动调用!

参数

  • string $type = NULL - Session 类型(native、cookie 等等)
  • string $id = NULL - Session 标识

Tags

返回值

  • Session

源代码

public static function instance($type = NULL, $id = NULL)
{
	if ($type === NULL)
	{
		// 使用默认类型
		$type = Session::$default;
	}
	if (!isset(Session::$instances[$type]))
	{
		// 为该类型加载配置信息
		$config = BootPHP::$config->load('session')->get($type);
		// 设置 session 类名
		$class = 'Session_' . ucfirst($type);
		// 创建一个新的 session 实例
		Session::$instances[$type] = $session = new $class($config, $id);
		// 关闭时写 session
		register_shutdown_function(array($session, 'write'));
	}
	return Session::$instances[$type];
}

public name( ) (在 Session 中定义)

Get the current session cookie name.

$name = $session->name();

返回值

  • string

源代码

public function name()
{
	return $this->_name;
}

public read( [ string $id = NULL ] ) (在 Session 中定义)

Loads existing session data.

$session->read();

参数

  • string $id = NULL - Session id

返回值

  • void

源代码

public function read($id = NULL)
{
	$data = NULL;
	try
	{
		if (is_string($data = $this->_read($id)))
		{
			if ($this->_encrypted)
			{
				// Decrypt the data using the default key
				$data = Encrypt::instance($this->_encrypted)->decode($data);
			}
			else
			{
				// Decode the base64 encoded data
				$data = base64_decode($data);
			}
			// Unserialize the data
			$data = unserialize($data);
		}
		else
		{
			// Ignore these, session is valid, likely no data though.
		}
	}
	catch (Exception $e)
	{
		// Error reading the session, usually
		// a corrupt session.
		throw new Session_Exception('Error reading session data.', NULL, Session_Exception::SESSION_CORRUPT);
	}
	if (is_array($data))
	{
		// Load the data locally
		$this->_data = $data;
	}
}

public regenerate( ) (在 Session 中定义)

Generates a new session id && returns it.

$id = $session->regenerate();

返回值

  • string

源代码

public function regenerate()
{
	return $this->_regenerate();
}

public restart( ) (在 Session 中定义)

Restart the session.

$success = $session->restart();

返回值

  • boolean

源代码

public function restart()
{
	if ($this->_destroyed === false)
	{
		// Wipe out the current session.
		$this->destroy();
	}
	// Allow the new session to be saved
	$this->_destroyed = false;
	return $this->_restart();
}

public set( string $key , mixed $value ) (在 Session 中定义)

Set a variable in the session array.

$session->set('foo', 'bar');

参数

  • string $key required - Variable name
  • mixed $value required - Value

返回值

  • $this

源代码

public function set($key, $value)
{
	$this->_data[$key] = $value;
	return $this;
}

public write( ) (在 Session 中定义)

Sets the last_active timestamp && saves the session.

$session->write();

Any errors that occur during session writing will be logged, but not displayed, because sessions are written after output has been sent.

Tags

返回值

  • boolean

源代码

public function write()
{
	if (headers_sent() || $this->_destroyed)
	{
		// Session cannot be written when the headers are sent || when
		// the session has been destroyed
		return false;
	}
	// Set the last active timestamp
	$this->_data['last_active'] = time();
	try
	{
		return $this->_write();
	}
	catch (Exception $e)
	{
		// Log & ignore all errors when a write fails
		BootPHP::$log->add(Log::ERROR, BootPHP_Exception::text($e))->write();
		return false;
	}
}

protected _destroy( ) (在 Session_Cookie 中定义)

返回值

  • bool

源代码

protected function _destroy()
{
	return Cookie::delete($this->_name);
}

protected _read( [ string $id = NULL ] ) (在 Session_Cookie 中定义)

参数

  • string $id = NULL - Session id

返回值

  • string

源代码

protected function _read($id = NULL)
{
	return Cookie::get($this->_name, NULL);
}

protected _regenerate( ) (在 Session_Cookie 中定义)

返回值

  • NULL

源代码

protected function _regenerate()
{
	// Cookie sessions have no id
	return NULL;
}

protected _restart( ) (在 Session_Cookie 中定义)

返回值

  • bool

源代码

protected function _restart()
{
	return true;
}

protected _write( ) (在 Session_Cookie 中定义)

返回值

  • bool

源代码

protected function _write()
{
	return Cookie::set($this->_name, $this->__toString(), $this->_lifetime);
}