PHP保存session默认的是采用的文件的方式来保存的,这仅仅在文件的空间开销很小的windows上是可以采用的,但是如果我们采用uinx或者是liux上的文件系统的时候,这样的文件系统的文件空间开销是很大的,然而session是要时时刻刻的使用的,大量的用户就要创建很多的session文件,这样对整个的服务器带来性能问题。
另一方面,如果服务器起采用群集的方式的话就不能保持session的一致性,所以我们就绪要采用数据库的方式来保存session,这样,不管有几台服务器同时使用,只要把他们的session保存在一台数据库服务器上就可以保证session的完整了,具体如何来实现请继续看下去。
PHP保存session默认的情况下是采用的文件方式来保存的,我们在PHP的配制文件PHP.ini中可以看到这样的一行,
session.save_handler="files"
这样的意思就是采用文件来保存session 的,要采用数据库来保存的话,我们需要修改成用户模式,改成
session.save_handler="use"
就可以了,但是,这仅仅是说明我门没有采用文件的方式存储session,我们还要选择数据库和建立数据库的表。
建立数据库和数据库的表结构,我们可以采用PHP可以使用的任何的数据库,因为PHP和mysql的结合最好,我就使用mysql来做示例,当然根据你的需要可以改称别的数据库。
创建数据库
create database 'session';
创建表结构
create table 'session'( id char(32) not null , 'user 'char(30), data char(3000) ,primary key ('id') );
PHP保存session编写PHP文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?php $con = mysql_connect( "127.0.0.1" , "user" , "pass" ); mysql_select_db( "session" ); function open( $save_path , $session_name ) { return (true); } function close() { return (true); } function read( $id ) { if ( $result = mysql_query( "select * from session where id='$id'" )) { if ( $row = mysql_felth_row( $result )) { return $row [ "data" ]; } } else { return "" ; } } function write( $id , $sess_data ) { if ( $result = mysql_query( "update session set data='$sess_data' where id='$id'" )) { return true; } else { return false; } } function destroy( $id ) { if ( $result = mysql_query( "delete * from session where id='$id'" )) { return true; } else { return false; } } function gc( $maxlifetime ) { return true; } session_set_save_handler( "open" , "close" , "read" , "write" , "destroy" , "gc" ); session_start(); // proceed to use sessions normally ?> |
保存成为session_user_start.php。
现在我们的PHP保存session的工作就已经完成了,只要你在需要在使用session的时候,把session_user_start.php包含进来.注意,这个文件一定要在文件的第一行包含,然后就像使用文件的session一样的方法使用就可以了。
以上仅仅是个简单教程,在实际的应用中,可以对它封装得更专业些,参考代码如下:
SessionMysql.class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | <?php /** * SessionMysql 数据库存储类 */ defined( 'IN_QIAN' ) or exit ( 'Access Denied' ); class SessionMysql { public $lifetime = 1800; // 有效期,单位:秒(s),默认30分钟 public $db ; public $table ; /** * 构造函数 */ public function __construct() { $this ->db = Base::loadModel( 'SessionModel' ); $this ->lifetime = Base::loadConfig( 'system' , 'session_lifetime' ); session_set_save_handler( array (& $this , 'open' ), // 在运行session_start()时执行 array (& $this , 'close' ), // 在脚本执行完成 或 调用session_write_close() 或 session_destroy()时被执行,即在所有session操作完后被执行 array (& $this , 'read' ), // 在运行session_start()时执行,因为在session_start时,会去read当前session数据 array (& $this , 'write' ), // 此方法在脚本结束和使用session_write_close()强制提交SESSION数据时执行 array (& $this , 'destroy' ), // 在运行session_destroy()时执行 array (& $this , 'gc' ) // 执行概率由session.gc_probability 和 session.gc_divisor的值决定,时机是在open,read之后,session_start会相继执行open,read和gc ); session_start(); // 这也是必须的,打开session,必须在session_set_save_handler后面执行 } /** * session_set_save_handler open方法 * * @param $savePath * @param $sessionName * @return true */ public function open( $savePath , $sessionName ) { return true; } /** * session_set_save_handler close方法 * * @return bool */ public function close() { return $this ->gc( $this ->lifetime); } /** * 读取session_id * * session_set_save_handler read方法 * @return string 读取session_id */ public function read( $sessionId ) { $condition = array ( 'where' => array ( 'session_id' => $sessionId ), 'fields' => 'data' ); $row = $this ->db->fetchFirst( $condition ); return $row ? $row [ 'data' ] : '' ; } /** * 写入session_id 的值 * * @param $sessionId 会话ID * @param $data 值 * @return mixed query 执行结果 */ public function write( $sessionId , $data ) { $userId = isset( $_SESSION [ 'userId' ]) ? $_SESSION [ 'userId' ] : 0; $roleId = isset( $_SESSION [ 'roleId' ]) ? $_SESSION [ 'roleId' ] : 0; $grouId = isset( $_SESSION [ 'grouId' ]) ? $_SESSION [ 'grouId' ] : 0; $m = defined( 'ROUTE_M' ) ? ROUTE_M : '' ; $c = defined( 'ROUTE_C' ) ? ROUTE_C : '' ; $a = defined( 'ROUTE_A' ) ? ROUTE_A : '' ; if ( strlen ( $data ) > 255) { $data = '' ; } $ip = get_ip(); $sessionData = array ( 'session_id' => $sessionId , 'user_id' => $userId , 'ip' => $ip , 'last_visit' => SYS_TIME, 'role_id' => $roleId , 'group_id' => $grouId , 'm' => $m , 'c' => $c , 'a' => $a , 'data' => $data , ); return $this ->db->insert( $sessionData , 1, 1); } /** * 删除指定的session_id * * @param string $sessionId 会话ID * @return bool */ public function destroy( $sessionId ) { return $this ->db-> delete ( array ( 'session_id' => $sessionId )); } /** * 删除过期的 session * * @param $lifetime session有效期(单位:秒) * @return bool */ public function gc( $lifetime ) { $expireTime = SYS_TIME - $lifetime ; return $this ->db-> delete ( "`last_visit`<$expireTime" ); } } ?> |
在系统文件的某个地方,实例化这个类即可!!!
new SessionMysql();