PHP实现Session入库/存入redis的方法

2025-05-29 0 91

对于大访问量的站点使用默认的Session 并不合适,我们可以将其存入数据库、或者使用Redis KEY-VALUE数据存储方案

首先新建一个session表

?

1

2

3

4

5

6
CREATE TABLE `sessions` (

`sid` char(40) NOT NULL,

`updatetime` int(20) NOT NULL,

`data` varchar(200) NOT NULL,

UNIQUE KEY `sid` (`sid`) USING HASH

) ENGINE=MEMORY DEFAULT CHARSET=utf8;

Mysql 的memory引擎采用内存表,所有数据存储在内存,操作速度快

?

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
<?php

//引入数据库文件

include "db.php";

class MySessionHandler implements SessionHandlerInterface

{

private $savePath;

private $sessData;

public $expiretime; //设置过期时间

public $db; //数据库

public function __construct($hanlder =''){

$this->db = Database::getInstance();

//获取数据库实力

///var_dump($this->db);

}

public function open($savePath, $sessionName)

{

return true;

}

public function close()

{

return true;

}

public function read($id)

{

$sql ="select * from sessions where sid ='$id'";

$result = $this->db->execute($sql);

if(!empty($result)){

return $this->sessData = $result;

}

}

//函数的参数 $id -> 当前会话ID

//数据DATA -> 序列化之后的字符串

public function write($id, $data)

{

// echo $id;

// echo $data;

$now = time();

$newExp = $now+$this->expiretime; //总时间=当前时间 + 期限时间

$sql = "select * from sessions where sid ='$id'";

$result = $this->db->getOne($sql);

//var_dump($result);

if($data==''||isset($data)){

$data = $this->sessData;

}

if($result){

//如果存在则更新

$sql ="update sessions set updatetime = '$newExp',data ='$data' where sid = '$id'";

//echo $sql;

$update_data =$this->db->execute($sql);

if($update_data){

return true;

}

}else{

//不存在则生成生成

$sql = "insert into sessions(sid,updatetime,data) values('$id','$now','$data')";

$insert_data = $this->db->execute($sql);

if($insert_data){

return true;

}

}

return false;

}

public function destroy($id)

{ //销毁

$sql = "delete from sessions where sid="."$id";

$destory = $this->db->execute($sql);

if($destory){

return true;

}else{

return false;

}

}

public function gc($sessMaxLifeTime)

{

$t = time();

$sql ="delete from sessions where $t - 'updatetime'>${sessMaxLifeTime}";

$data = $this->db->execute($this->tosql);

if($data){

return true;

}else{

return false;

}

return true;

}

}

实例化

此处 PHP 手册可以有两种方法

1,实现了SessionHandlerInterface借口的对象,自PHP5.4可以使用

2 ,直接使用 session_set_save_handler

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
//判断PHP版本

if(version_compare(PHP_VERSION,5.4)==1){

session_set_save_handler($handler, true);

session_start();

}else{

ini_set('session.use_trans_sid',0);

ini_set('session.use_cookies',1);

ini_set('session.cookie_path','/');

ini_set('session.save_handler','user');

session_module_name('user');

session_set_save_handler(array($session,"open"),array($session,"close"),array($session,"read"),array($session,"write"),array($session,"destory"),array($session,"gc"));

session_start();

}

$_SESSION['QQ']="QQ";

echo $_SESSION['QQ'];

数据库代码 db.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
<?php

class Database{

static $instance;

static $db;

static function getInstance(){

if(self::$instance){

return self::$instance;

}else{

return new Database();

}

}

public function __construct(){

self::$db = new PDO('mysql:host=localhost;dbname=session', 'root','');

}

public function getOne($sql){

$rs =self::$db->query($sql);

@$rs->setFetchMode(PDO::FETCH_ASSOC);//返回关联数组

$result = $rs -> fetch();

return $result;

}

public function execute($sql){

$rs = self::$db->exec($sql);

return $rs;

}

}

//$data = Database::getInstance();

//var_dump($data);

使用REDIS 存储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
<?php

class SessionManager{

private $redis;

private $sessionSavePath;

private $sessionName;

private $sessionExpireTime = 30;

public function __construct(){

$this->redis = new Redis();

$this->redis->connect('127.0.0.1',6379); //连接redis

$retval = session_set_save_handler(

array($this,"open"),

array($this,"close"),

array($this,"read"),

array($this,"write"),

array($this,"destory"),

array($this,"gc")

);

session_start();

}

public function open($path,$name){

return true;

}

public function close(){

return true;

}

public function read($id){

$value = $this->redis->get($id);

if($value){

return $value;

}else{

return "";

}

}

public function write($id,$data){

if($this->redis->set($id,$data)){

$this->redis->expire($id,$this->sessionExpireTime);

//设置过期时间

return true;

}

return false;

}

public function destory($id){

if($this->redis->delete($id)){

return true;

}

return false;

}

public function gc($maxlifetime){

return true;

}

//析构函数

public function __destruct(){

session_write_close();

}

}

$re = new SessionManager();

$_SESSION['name'] = "qq";

echo $_SESSION['name'];

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/yhdsir/p/5529162.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 PHP实现Session入库/存入redis的方法 https://www.kuaiidc.com/94620.html

相关文章

发表评论
暂无评论