apache - how to create session in php with mongodb -


in node.js create mongodb session this.

 app.configure(function()  {  app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(express.bodyparser()); app.use(express.cookieparser()); app.use(express.session({     secret: 'my secret',     store: new mongostore({         db: 'my session db',         host: 'localhost',         port:88888     }) })); app.use(everyauth.middleware()); app.use(express.methodoverride());  app.use(app.router); 

});

how create session mongodb in php.i new 1 php..i want create session mongodb in php(webserver: apache),so let me know how create

you must use session handler accomplish this. don't answer these sort of questions lack research of kind but, 1 time, here small, extremely simple, self contained edition have:

class session{      public $db;      /**      * decides lifetime (in seconds) of session      *      * @access private      * @var int      */     public $life_time='+2 weeks';       /**      * stores found session collection don't      * waste resources going      *      * @access private      * @var sessions      */     private $_session = array();      /**      * constructor      */     function open() {          // ensure index on session id         $this->db->sessions->ensureindex(array('session_id' => 1), array("unique" => true));          // register object session handler         session_set_save_handler(             array( $this, "opensession" ),             array( $this, "closesession" ),             array( $this, "readsession" ),             array( $this, "writesession"),             array( $this, "destroysession"),             array( $this, "gcsession" )         );         session_start(); // start damn session     }      /**      * open session      *      * function opens session save path.      * save path can changed method of opening can      * not change basics , return      *      * @param string $save_path      * @param string $session_name      */     function opensession( $save_path, $session_name ) {          global $sess_save_path;          $sess_save_path = $save_path;          // don't need anything. return true.         return true;      }      /**      * function closes session (end of session)      */     function closesession() {          // return true indicate session closed         return true;      }      /**      * read function called when open session.      * function attempts find session db. if cannot      * session class variable remain null.      *      * @param string $id      */     function readsession( $id ) {          // set empty result         $data = '';          // fetch session data selected database         $time = time();          $this->_sessions = $this->db->sessions->findone(array("session_id"=>$id));          if (!empty($this->_sessions)) {             $data = $this->_sessions['session_data'];         }          return $data;      }      /**      * write function. called when session closes ,      * writes new data db. 2 actions depending on whether or not      * session exists. if session exist update session      * otherwise insert new session.      *      * @param string $id      * @param mixed $data      *      * @todo need make function aware of other users since php sessions not unique maybe delete old sessions.      */     function writesession( $id, $data ) {          //write details session table         $time = strtotime('+2 weeks');          // if user logged in record uid         $uid = $_session['logged'] ? $_session['uid'] : 0;          $fields = array(             "session_id"=>$id,             "user_id"=>$uid,             "session_data"=>$data,             "expires"=>$time,             "active"=>1         );          $fg = $this->db->sessions->update(array("session_id"=>$id), array('$set'=>$fields), array("upsert"=>true));          // done         return true;     }      /**      * function called when user calls session_destroy().      * kills session , removes it.      *      * @param string $id      */     function destroysession( $id ) {          // remove db         $this->db->sessions->remove(array("session_id" => $id), true);          return true;     }      /**      * function gcs (garbage collection) old , out of date sessions      * still exist in db. remove comparing current time of      * expiring on session record.      *      * @todo make cronjob delete sessions after day old , still inactive      */     function gcsession() {         $this->db->sessions->remove(array('expires' => array('$lt' => strtotime($this->life_time))));         return true;     } } 

which can called so:

$session = new session; $session->db=$mongo->my_db; $session->open(); 

it basic example of how this.

afterwards can use normal session so:

$_session['user_id'] = $id; 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -