mysql-sessionhandler

Gesponsorde koppelingen

PHP script bestanden

  1. mysql-sessionhandler

« Lees de omschrijving en reacties

session.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/* Title: Session handler
 * Author: Jurgen Meijer
 * E-mail: [email protected]
 *
 * Date: 19-04-2008
 * License: GNU/GPL
 *
 * PHP version: 4+
 * MySQL version 4+
 *
 * This is a session handler written for PHP5 but may work in PHP4 aswell. This version is a mysql only version.
 * This handler is only tested in PHP5 and MySQL 5.
 *
 * Note: session.auto_start should be turned off in the php.ini file to allow custom sessionhandling.
 *
 * SQL:
 *
    CREATE TABLE session (
    session_id VARCHAR( 32 ) NOT NULL ,
    session_data MEDIUMTEXT NOT NULL ,
    session_garbage TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL ,
    PRIMARY KEY (session_id)
    ) ENGINE = innodb;
*/


function open_session()
{

    //Global variable to be used within all the functions.
    global $db;
    
    //Connect to the database.
    if($db = mysql_connect('localhost', 'root',''))
    {

        //if succeeded select the database.
        mysql_select_db('sessionhandler');
        return true;
    }

    else
    {
        return false;
    }
}

function
close_session()
{

    //Global variable to be used within all the functions.
    global $db;
    
    //Close the connection to the database server by setting the $db variable to NULL.
    $db = NULL;
    return true;
}

function
read_session($session_id)
{

    //Global variable to be used within all the functions.
    global $db;
    
    //Selecting the data from the database where the current session_id equals the one in the database.
    $query = "SELECT
                session_data
          FROM
                  session
          WHERE
                  session_id='"
.$session_id."' ";
    
    //check if the query succeedes.
    if($result = mysql_query($query))
    {

        //If a row meets the requirement list the data and return it.
        if(mysql_num_rows($result) == 1)
        {

            list($data) = mysql_fetch_array($result, MYSQL_NUM);
            return $data;
        }

        else
        {
            //If there is no result, send back an empty string.
            return '';
        }
    }

    else
    {
        //If the query fails, send back an empty string.
        return '';
    }    
}

function
write_session($session_id, $session_data)
{

    //Global variable to be used within all the functions.
    global $db;
        
    //Replace the session_data with the new data.
    
    //* Replace is a SQL function that works as an INSERT/UPDATE.
    //If there is no row, create a new one, else update the current.

    $query = " REPLACE INTO
                            session
                            (
                            session_id,
                            session_data,
                            session_garbage
                            )
                            VALUES
                            (
                            '"
.$session_id."' ,
                            '"
.$session_data."',
                            CURRENT_TIMESTAMP)"
;

    
        if(mysql_query($query))
        {

            //if the query succeedes return the affected rows (should allways be 1).
            return mysql_affected_rows();
        }

        else
        {
            return false;
        }
                        
}

function
destroy_session($session_id)
{

    //Global variable to be used within all the functions.
    global $db;
    
    //Delete our session, this function will be called when we use session_destroy().
    $query = "DELETE FROM
                        session
              WHERE
                      session_id='"
.$session_id."'";
    
    //If the query succeedes empty the $_SESSION array and return the affected rows.
    if(mysql_query($query))
    {

        $_SESSION = array();
        return mysql_affected_rows();
    }

    else
    {
        return false;
    }
    
}

function
clean_session($seconds)
{

    //Global variable to be used within all the functions.
    global $db;
    
    //Delete items older than the amount of seconds wich is defined in the php.ini file.
    //Default is after the browser is closed(0 seconds).

    $query = "DELETE FROM
                    session
              WHERE
                     DATE_ADD(session_garbage, INTERVAL "
.$seconds." SECOND) < NOW()";
    
    //Return the affected rows when the query succeedes.
    if(mysql_query($query))
    {

        return mysql_affected_rows();
    }

    else
    {
        return false;
    }
    
}



//Set our brand new sessionhandler to be used by PHP.
session_set_save_handler('open_session','close_session', 'read_session', 'write_session', 'destroy_session', 'clean_session');

//Start the session as you would normally do.
session_start();
?>


En het aanroepen van het script:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
<?php

require 'session.php';

$_SESSION['blaat'] = 'LOL';
$_SESSION['schaap'] = 1;


session_write_close();

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.