session存入数据库

来源:互联网 发布:网络架构图怎么做 编辑:程序博客网 时间:2024/06/08 16:50
我们首先写一个存储类(注意这里没有对DB操作进行封装),暂叫session.class.php:

<?php

class session_mysql

{

private $_db_link;

private $_table;

//这个是SESSION的回收周期(秒)

private $_gc_lifetime = 30;

public function __construct($host, $user, $pwd, $db, $table = 'session', $sessionName = '', $cookieLife = 0)

{
session_module_name("user");/设置session保持方式,或者ini_set('session.save_handler','user')
 

$this->_db_link = mysql_connect ( $host, $user, $pwd );

if (! $this->_db_link)

{

return False;

}

if (mysql_query ( "USE $db" ))

{

$this->_table = $table;

session_set_save_handler ( array (&$this, 'open' ), array (&$this, 'close' ), array (&$this, 'read' ), array (&$this, 'write' ), array (&$this, 'destroy' ), array (&$this, 'gc' ) );

//周期

$cookieLife = intval ( $cookieLife );

if ($cookieLife > 0)

{

session_set_cookie_params ( $cookieLife );

}

if ($this->_gc_lifetime > 0)

{

ini_set ( 'session.gc_maxlifetime', $this->_gc_lifetime );

} else

{

$this->_gc_lifetime = ini_get ( 'session.gc_maxlifetime' );

}

//名称

if (! empty ( $sessionName ))

{

ini_set ( 'session.name', $sessionName );

}

return session_start ();

}

return False;

}

public function open($save_path, $session_name)

{

return true;

}

public function close()

{

//删除过期的SESSION

$this->gc ( $this->_gc_lifetime );

//关闭数据库

//(注意如果系统其它功能和SESSION共用一个数据库,此处关闭可能会影响到其它功能,根据实际情况而定)

return mysql_close ( $this->_db_link );

}

public function read($id)

{

$id = mysql_real_escape_string ( $id );

$sql = "select `data` from `$this->_table` where `id`='$id' limit 1";

if ($result = mysql_fetch_assoc ( mysql_query ( $sql, $this->_db_link ) ))

{

return $result ['data'];

}

return '';

}

public function write($id, $data)

{

$time = time ();

$id = mysql_real_escape_string ( $id );

$data = mysql_real_escape_string ( $data );

$sql = "replace into `$this->_table` values('$id','$data',$time)";

return mysql_query ( $sql, $this->_db_link );

}

public function destroy($id)

{

$sql = "delete from `$this->_table` where `id`='$id' limit 1";

return mysql_query ( $sql, $this->_db_link );

}

public function gc($lifetime)

{

$expire = time () - $lifetime;

$sql = "delete from `$this->_table where `created_time` < $expire";

return mysql_query ( $sql, $this->_db_link );

}

}

 

接着我们写一个公用的方法,暂叫function.php:

<?php

function my_session_start($sessionName = '', $lifetime = 0)

{

//这里装载SESSION类(由于这里方便演示,文件结构分布不严谨)

require_once './session.class.php';

//配置好相关内容

$session = new session_mysql ( '127.0.0.1', 'xxx', 'xxx', 'test', 'session', $sessionName, $lifetime );

if ($session)

{

return True;

}

return False;

}

 

最后,我们来测试一下效果,test.php:

<?php

//测试一下

require_once './function.php';

error_reporting ( E_ALL );

my_session_start ();

$_SESSION ['test'] = 'test';

$_SESSION ['testtwo'] = array ('testtwo' );


我们再在另外一个页面输出SESSION,test2.php

<?php

require_once './function.php';

error_reporting ( E_ALL );

my_session_start ();

var_dump($_SESSION);

 

运行后我们可以看到:

array 'test' =>string'test'(length=4) 'testtwo' =>array 0 =>string'testtwo'(length=7)


 恩,一切正常。我们再看一下数据库中是什么:



恩,浏览器里又是怎么样的呐(这里只测试了FIREFOX,在生产时请务必各浏览器都要测试一下)?




恩,好了以就是一个简单的例子,那么如果我们要使用特殊的SESSION NAME或者要求有过期时间呐?test.php

<?php

//测试一下特殊名称

require_once './function.php';

error_reporting ( E_ALL );

my_session_start ( 'testhehe', 60 );//一分钟

$_SESSION ['lifettimetest'] = 'ok';

 

恩,我们来获取看看:test2.php

<?php

//测试一下特殊名称

require_once './function.php';

error_reporting ( E_ALL );

my_session_start ( 'testhehe' );

var_dump ( $_SESSION );


恩,我们来输出:

array 'lifettimetest' =>string'ok'(length=2)恩,貌似正常,我们再看看数据库与浏览器里的值:(数据库)


这样看来我们测试成功了。好了,以上就是关于session_set_save_handler的操作方法了。