使用mysql保存session,并建立在线用户列表

作者 : admin 于 2008-08-15 23:02:21 标签: , , ,
2008
08-15

写OA,使用mysql存储session,这样,是为了获得在线用户的列表,为做一个web im做准备。

首先创建表,注,这里使用了adodb中数据字典的描述方法:

  1. $Session_Fields = "
  2. session_id VARCHAR(100) NOTNULL,
  3. session_value X DEFAULT '',
  4. session_expires I(10) DEFAULT NULL,
  5. userid I(5) DEFAULT NULL
  6. ";
  7. $Session_Tables = array('mysql' => "ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_unicode_ci COMMENT = 'session表'");
  8. $dict = NewDataDictionary(GetDB());
  9. $sqlarray = $dict->CreateTableSQL($_CFG['table']['session'], $Session_Fields, $Session_Tables);
  10. $dict->ExecuteSQLArray($sqlarray);

然后session类:

  1. <?php
  2. /*  Session.Class.php
  3.  *  Session Manager
  4.  *  @link        http://www.sunboyu.cn
  5.  *  @package     OA
  6.  *  @version     V1.0
  7.  *
  8.  *  2008 08 14  sunboyu@gmail.com
  9.  */
  10. class Session
  11. {
  12. var $lifetime;
  13. var $db = null;
  14. function __construct()
  15. {
  16. #return adodb lite connetction
  17. $this->db = GetDB();
  18. }
  19. #open
  20. function open( $savepath , $session_name )
  21. {
  22. $this->lifetime =  get_cfg_var("session.gc_maxlifetime");
  23. return true;
  24. }
  25. #close
  26. function close()
  27. {
  28. return true;
  29. }
  30. #read
  31. function read( $session_id )
  32. {
  33. global $_CFG;
  34. $sql = sprintf("SELECT * FROM %s WHERE session_id = %s",$_CFG['table']['session'],GetSqlString( $session_id ));
  35. $rs = $this->db->Execute( $sql );
  36. if($rs->RecordCount()>0)
  37. {
  38. $result = $rs->fields;
  39. return $result['session_value'];
  40. }
  41. else
  42. {
  43. return null;
  44. }
  45. }
  46. #write
  47. function write( $session_id , $session_value )
  48. {
  49. global $_CFG;
  50. $sql = sprintf("SELECT session_id FROM %s WHERE session_id = %s",$_CFG['table']['session'],GetSqlString( $session_id ));
  51. $rs = $this->db->Execute( $sql );
  52. if($rs->RecordCount()==0)
  53. {
  54. $newsql = sprintf("INSERT INTO %s SET session_id = %s , session_value = %s , session_expires = %d",
  55. $_CFG['table']['session'],
  56. GetSqlString( $session_id ),
  57. GetSqlString( $session_value ),
  58. GetSqlString( $this->lifetime+time() , "int" ));
  59. }
  60. else
  61. {
  62. $newsql = sprintf("UPDATE %s SET session_value = %s , session_expires = %d WHERE session_id = %s",
  63. $_CFG['table']['session'],
  64. GetSqlString( $session_value ),
  65. GetSqlString( $this->lifetime+time() , "int" ),
  66. GetSqlString( $session_id ));
  67. }
  68. return $this->db->Execute( $newsql );
  69. }
  70. #destroy
  71. function destroy( $session_id )
  72. {
  73. global $_CFG;
  74. $delsql = sprintf("DELETE FROM %s WHERE session_id = %s",$_CFG['table']['session'],GetSqlString( $session_id ));
  75. return $this->db->Execute( $delsql );
  76. }
  77. #gc
  78. function gc()
  79. {
  80. global $_CFG;
  81. $gcsql = sprintf("DELETE FROM %s WHERE session_expires < %d",$_CFG['table']['session'],time());
  82. return $this->db->Execute( $gcsql );
  83. }
  84. }
  85. ?>

session机制,之前已经介绍,实现代码如下

  1. $session = new Session();
  2. session_set_save_handler(array(&$session,"open"),
  3. array(&$session,"close"),
  4. array(&$session,"read"),
  5. array(&$session,"write"),
  6. array(&$session,"destroy"),
  7. array(&$session,"gc"));
  8. session_start();

把session与用户id对应,使用以下语句即可

  1. sprintf("UPDATE %s SET userid = %d WHERE session_id = %s",$_CFG['table']['session'],GetSqlString($rs['id'],'int'),GetSqlString(session_id(),'text'))

发表评论




XHTML:你可以使用的标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

(若看不到验证码,请重新加载页面。)