Table of Contents
2. [代码][PHP]代码  
Home php教程 PHP源码 memcache存储session 简单版本 实现功能了 空了来修改~

memcache存储session 简单版本 实现功能了 空了来修改~

May 25, 2016 pm 05:13 PM

跳至

1

2

3

4

include('cls_session.php');

$m =new Memcache;

$m->connect('localhost',11211);   

$sess = new cls_session($m);

Copy after login

2. [代码][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

session_name       = $session_name;

 

        $this->db  = &$db;

        $this->_ip = '127.0.0.1';

 

        if ($session_id == '' && !empty($_COOKIE[$this->session_name]))

        {

            $this->session_id = $_COOKIE[$this->session_name];

        }

        else

        {

            $this->session_id = $session_id;

        }

 

        if ($this->session_id)

        {

            $tmp_session_id = substr($this->session_id, 0, 32);

            if ($this->gen_session_key($tmp_session_id) == substr($this->session_id, 32))

            {

                $this->session_id = $tmp_session_id;

            }

            else

            {

                $this->session_id = '';

            }

        }

 

        $this->_time = time();

 

        if ($this->session_id)

        {

 

            $this->load_session();

        }

        else

        {

 

            $this->gen_session_id();

 

            setcookie($this->session_name, $this->session_id . $this->gen_session_key($this->session_id), 0);

        }

 

        register_shutdown_function(array(&$this, 'close_session'));

    }

 

    function gen_session_id()

    {

        $this->session_id = md5(uniqid(mt_rand(), true));

 

        return $this->insert_session();

    }

 

    function gen_session_key($session_id)

    {

        static $ip = '';

 

        if ($ip == '')

        {

            $ip = substr($this->_ip, 0, strrpos($this->_ip, '.'));

        }

 

        return sprintf('%08x', crc32(!empty($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] . ROOT_PATH . $ip . $session_id : ROOT_PATH . $ip . $session_id));

    }

 

    function insert_session()

    {

        $this->db->set(

            $this->session_id,

            array(

                'ip'   => $this->_ip,

                'time' => $this->_time,

            ),

            false,

            $this->max_life_time

        );

         

    }

 

    function load_session()

    {

        $GLOBALS['_SESSION'] = $this->db->get($this->session_id);

    }

 

    function update_session()

    {

        $this->db->replace($this->session_id,$GLOBALS['_SESSION'],$this->max_life_time);

    }

 

    function close_session()

    {

        $this->update_session();

    }

 

    function get_session_id()

    {

        return $this->session_id;

    }

    function destroy_session()

    {   

        setcookie($this->session_name,'', 0);

        $this->db->delete($this->session_id);

    }

}

Copy after login

           

       

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1665
14
PHP Tutorial
1270
29
C# Tutorial
1250
24