11.4将session存储到数据库

11.4.1问题
You want to store session data in a database instead of in files. If multiple web servers all have access to the same database, the

session data is then mirrored across all the web servers.
你想要存储session数据在一个数据库代替在文件里。如果多样化网络服务器都有权使用一样的数据库。这个session数据在这个DOS访问所有 web服务器
11.4.2解答
Use a class or a set of functions in conjunction with the session_set_save_handler( ) function to define database-aware routines for

session management. For example, use PEAR’s HTTP_Session package for convenient database session storage:

使用一个类或者一个函数集和这个session_set_save_handler()函数定义database-aware程序管理、例如。使用REAR’s HTTP_Session 便利的存储session到

数据库


?php
require_once 'HTTP/Session/Container/DB.php';
$s = new HTTP_Session_Container_DB('mysql://user:password@localhost/db');
ini_get('session.auto_start') or session_start();

11.4.3讨论
One of the most powerful aspects of the session module is its abstraction of how sessions get saved. The session_set_save_handler( )

function tells PHP to use different functions for the various session operations such as saving a session and reading session data.

一个session 模块最有力的方面是怎么获得session.这个session_set_save_handler()函数告诉PHP去使用不同的函数在不同的session操作。例如获得一个

session和读出session的数据
The PEAR HTTP_Session package provides classes that take advantage of PEAR’s DB, MDB, and MDB2 database abstraction packages to store

session data in a database. If the database is shared between multiple web servers, users’ session information is portable across all

those web servers. So if you have a bunch of web servers behind a load balancer, you don’t need any fancy tricks to ensure that a user’s

session data is accurate no matter which web server she gets sent to.

这个PEAR HTTP_Session 规定利用PEAR’sDB 和MDB2数据库提交数据库包去存储session数据,如果这个数据库共享多个浏览器。使用者的session信息轻松越

过这些服务器。所有如果你有一个网络服务器的加载权衡,你不需要一些特别的窍门去确保使用者的session数据正确性

To use HTTP_Session_Container_DB, pass a data source name (DSN) to the class when you instantiate it. The session data is stored in a

table called sessiondata whose structure is:
去使用HTTP_Session_Container_DB。通过一个数据发起姓名(DSN)到这个类。当你示例时。这个session数据存储在一个sessiondata的表格里


CREATE TABLE sessiondata
(
  id CHAR(32) NOT NULL,
  data MEDIUMBLOB,
  expiry INT UNSIGNED NOT NULL,
  PRIMARY KEY (id)
);

If you want the table name to be different than sessiondata, you can set a new table name with an options array when instantiating the

HTTP_Session_Container_DB class:

如果你想要表格名字不同于sessiondata。 你可以设置一个新的表格名和一个选项数组当示例这个HTTP_Session_Container_DB类


?php
require_once 'HTTP/Session/Container/DB.php';
$options = array(
  'table'  => 'php_session',
  'dsn'    => 'mysql://user:password@localhost/db'
);
$s = new HTTP_Session_Container_DB($options);
ini_get('session.auto_start') or session_start();

Comments are closed.