root/trunk/trax/vendor/trax/session/active_record_store.php

Revision 300, 3.0 KB (checked in by john, 4 years ago)

fixed a few bugs with new session AR store

Line 
1<?php
2/**
3 *  File containing the ActiveRecord Session Store class
4 *
5 *  (PHP 5)
6 *
7 *  @package PHPonTrax
8 *  @version $Id:$
9 *  @copyright (c) 2005 John Peterson
10 *
11 *  Permission is hereby granted, free of charge, to any person obtaining
12 *  a copy of this software and associated documentation files (the
13 *  "Software"), to deal in the Software without restriction, including
14 *  without limitation the rights to use, copy, modify, merge, publish,
15 *  distribute, sublicense, and/or sell copies of the Software, and to
16 *  permit persons to whom the Software is furnished to do so, subject to
17 *  the following conditions:
18 *
19 *  The above copyright notice and this permission notice shall be
20 *  included in all copies or substantial portions of the Software.
21 *
22 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31/**
32 * A session store backed by an Active Record class.
33 */
34
35/**
36 *
37 * Session Table Schema:
38 *
39 * CREATE TABLE sessions (
40 *   id varchar(100) NOT NULL default '',
41 *   client_ip varchar(20) default NULL,
42 *   http_user_agent varchar(150) default NULL,
43 *   data text default NULL,
44 *   created_at datetime default NULL,
45 *   updated_at datetime default NULL,
46 *   PRIMARY KEY (id)
47 * )
48 *
49 */
50class ActiveRecordStore extends ActiveRecord {
51   
52    public $table_name = 'sessions';
53   
54    function open($save_path, $session_name) {
55        return true;   
56    }
57
58    function close() {
59        return true;
60    }
61
62    function read($sess_id) {
63        $data = '';
64        # Select the data belonging to session $sess_id from the session table
65        if(($session = $this->find($sess_id)) instanceof ActiveRecordStore) {
66            $data = $session->data;
67        }       
68        return $data;
69    }
70
71    function write($sess_id, $data) {
72        # Select the data belonging to session $sess_id from the session table
73        $session = $this->find($sess_id);
74        $session = ($session instanceof ActiveRecordStore) ? $session : $this;
75        $session->id = $sess_id;
76        $session->data = $data;
77        $session->client_ip = $this->escape($_SERVER['REMOTE_ADDR']);
78        $session->http_user_agent = $this->escape($_SERVER['HTTP_USER_AGENT']);
79        # Write the serialized session data ($data) to the session table
80        return $session->save() ? true : false; 
81    }
82
83    function destroy($sess_id) {
84        // Delete from the table all data for the session $sess_id
85        return $this->delete($sess_id) ? true : false;
86    }
87
88    function gc($max_lifetime) {
89        # Old values are values with a Unix less than now - $max_lifetime
90        $old = time() - (Trax::$session_maxlifetime_minutes * 60);
91        # Delete old values from the MySQL session table
92        $this->delete_all("UNIX_TIMESTAMP(created_at) < {$old}");
93        return true;
94    }
95       
96}
97
98
99?>
Note: See TracBrowser for help on using the browser.