Example #1
0
 public function write($sid, $data)
 {
     Session::emit('log', 'Saving session[' . $sid . ']:' . json_encode($data));
     preg_match('/[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}\\.[\\d]{1,3}/', $_SERVER['REMOTE_ADDR'], $match);
     $ip = sizeof($match) ? $_SERVER['REMOTE_ADDR'] : '0.0.0.0';
     switch (Session::$hook) {
         case 'php':
             //this means are delivered serialized code from the parent
             $data = session_real_decode($data);
             break;
         case 'custom':
     }
     $expiry = time() + Session::$lifetime;
     switch (Session::$source) {
         case 'mysql':
             if (is_array($data)) {
                 $data = json_encode($data);
             }
             $data = mysql_real_escape_string($data, Session::$link);
             $sql = "INSERT INTO " . Session::$sessionStore . " (session_id, data, session_time, ip, creation_time, modification_time )\n                        VALUES ('{$sid}', '" . $data . "', {$expiry}, '{$ip}', now(), now() ) \n                        ON DUPLICATE KEY UPDATE data='" . $data . "', session_time = '{$expiry}', modification_time = now()";
             //echo('|'.$sql.'|');
             $results = mysql_query($sql, Session::$link);
             break;
         case 'mysqli':
             if (is_array($data)) {
                 $data = json_encode($data);
             }
             $data = Session::$link->real_escape_string($data);
             $sql = "INSERT INTO " . Session::$sessionStore . " (session_id, data, session_time, ip, creation_time, modification_time )\n                        VALUES ('{$sid}', '{$data}', {$expiry}, '{$ip}', now(), now() ) \n                        ON DUPLICATE KEY UPDATE data='{$data}', session_time = '{$expiry}', modification_time = now()";
             $results = Session::$link->query($sql);
             break;
         case 'mongo':
             $update = array('data' => $data);
             unset($update['session_id']);
             //don't reset the key
             unset($update['_id']);
             $coll = Session::$sessionStore;
             $collection = Session::$link->{$coll};
             $update['session_time'] = time();
             $res = $collection->update(array('session_id' => $sid), array('$set' => $update), array('upsert' => true));
             break;
         case 'memcache':
             if (is_array($data)) {
                 $data = json_encode($data);
             }
             Session::$link->set($sid, $data, $expiry);
             break;
     }
     return true;
 }