gpt4 book ai didi

PHP封装的数据库保存session功能类

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章PHP封装的数据库保存session功能类由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

本文实例讲述了PHP封装的数据库保存session功能类。分享给大家供大家参考,具体如下:

PHP用数据库保存session类:

?
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
<?php
class SafeSessionHandler implements SessionHandlerInterface {
   public $save_path ;
   public $session_name ;
   public $table ;
   public function __construct() {
     $this ->table = new Table( "safe_session" );
   }
   private function session_id_parse( $session_id ) {
     $time = hexdec( substr ( $session_id , 0, 8));
     $skey = substr ( $session_id , 8);
     return array ( $time , $skey );
   }
   public function close() {
     loginfo( "close: " );
     return true;
   }
   public function create_sid() {
     loginfo( "create_sid: " );
     $time = time();
     $skey = "" ;
     $char = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
     for ( $i =0; $i <52; $i ++) {
       $skey .= $char {mt_rand(0, 61)};
     }
     $session = array (
       "time" => $time ,
       "skey" => $skey ,
       "sval" => "" ,
     );
     $this ->table->insert( $session );
     return dechex ( $time ) . $skey ;
   }
   public function destroy( $session_id ) {
     loginfo( "destroy: %s" , $session_id );
     list( $time , $skey ) = $this ->session_id_parse( $session_id );
     $this ->table->where( "time = ?" , $time )->where( "skey = ?" , $skey )-> delete ();
     return true;
   }
   public function gc( $maxlifetime ) {
     loginfo( "gc: %s" , $maxlifetime );
     $this ->table->where( "time < ?" , time() - 86400 * 30)-> delete ();
     return true;
   }
   public function open( $save_path , $session_name ) {
     loginfo( "open: %s, %s" , $save_path , $session_name );
     $this ->save_path = $save_path ;
     $this ->session_name = $session_name ;
     return true;
   }
   public function read( $session_id ) {
     loginfo( "read: %s" , $session_id );
     list( $time , $skey ) = $this ->session_id_parse( $session_id );
     $row = $this ->table->where( "time = ?" , $time )->where( "skey = ?" , $skey )->select()->fetch();
     if ( empty ( $row )) {
       return "" ;
     }
     return $row [ "sval" ];
   }
   public function write( $session_id , $session_data ) {
     loginfo( "write: %s, %s" , $session_id , $session_data );
     $session = array ( "sval" => $session_data ,);
     list( $time , $skey ) = $this ->session_id_parse( $session_id );
     $this ->table->where( "time = ?" , $time )->where( "skey = ?" , $skey )->update( $session );
     return true;
   }
}

希望本文所述对大家PHP程序设计有所帮助.

最后此篇关于PHP封装的数据库保存session功能类的文章就讲到这里了,如果你想了解更多关于PHP封装的数据库保存session功能类的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com