- Added more function to ActiveMongo (getArray(), servedFromCache())
[activemongo.git] / samples / sessions / sessions.php
blob3ecfa08324cb6052eab502487e2c7debc00837d7
1 <?php
2 /*
3 +---------------------------------------------------------------------------------+
4 | Copyright (c) 2010 ActiveMongo |
5 +---------------------------------------------------------------------------------+
6 | Redistribution and use in source and binary forms, with or without |
7 | modification, are permitted provided that the following conditions are met: |
8 | 1. Redistributions of source code must retain the above copyright |
9 | notice, this list of conditions and the following disclaimer. |
10 | |
11 | 2. Redistributions in binary form must reproduce the above copyright |
12 | notice, this list of conditions and the following disclaimer in the |
13 | documentation and/or other materials provided with the distribution. |
14 | |
15 | 3. All advertising materials mentioning features or use of this software |
16 | must display the following acknowledgement: |
17 | This product includes software developed by César D. Rodas. |
18 | |
19 | 4. Neither the name of the César D. Rodas nor the |
20 | names of its contributors may be used to endorse or promote products |
21 | derived from this software without specific prior written permission. |
22 | |
23 | THIS SOFTWARE IS PROVIDED BY CÉSAR D. RODAS ''AS IS'' AND ANY |
24 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
25 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
26 | DISCLAIMED. IN NO EVENT SHALL CÉSAR D. RODAS BE LIABLE FOR ANY |
27 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
28 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
29 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
30 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
31 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
32 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE |
33 +---------------------------------------------------------------------------------+
34 | Authors: César Rodas <crodas@php.net> |
35 +---------------------------------------------------------------------------------+
38 class MongoSession extends ActiveMongo
40 protected static $session;
42 public $data;
43 public $sid;
44 public $valid;
45 public $ts;
47 function getCollectionName()
49 return 'session';
52 function setup()
54 $this->addIndex(array("sid" => 1, "valid" => 1));
55 $this->addIndex(array("ts" => 1));
58 function before_create(&$document)
60 $document['ts'] = new MongoDate();
63 final public static function init()
65 $class = get_called_class();
66 session_set_save_handler(
67 array($class, "Open"),
68 array($class, "Close"),
69 array($class, "Read"),
70 array($class, "Write"),
71 array($class, "Destroy"),
72 array($class, "GC")
74 self::$session = new $class;
77 final public static function Open($path, $name)
79 return TRUE;
82 final public static function Close()
84 self::$session = null;
85 return TRUE;
88 final public static function Read($id)
90 $session = self::$session;
91 $session->where('sid', $id)->where('valid', TRUE);
92 if ($session->count() == 0) {
93 $session->sid = $id;
94 $session->valid = TRUE;
95 $session->save();
97 return $session->data;
100 final public static function Write($id, $ses_data)
102 $session = self::$session;
103 $session->data = $ses_data;
104 $session->ts = new MongoDate();
105 $session->save(FALSE);
107 return TRUE;
110 final public static function Destroy($id)
112 $session = self::$session;
113 $session->delete();
116 final public static function GC($max_time)
118 $class = get_called_name();
119 $sessions = new $class;
120 $session->delete_old_sessions($max_time);
123 function delete_old_sessions($max_time)
125 $filter = array(
126 'ts' => array(
127 '$lt' => new MongoDate(time()-$max_time),
130 $this->_getCollection->remove($filter);