Friday, 27 May 2016

Loading

Code Igniter - Session Management

Code Igniter - Session Management

 The Session class permits you maintain a user’s “status” and track their activity while browser.
 Code Igniter comes with a few session storage drivers:
·         database
·         files (default; file-system based)
·         redis
·         memcached
Database Driver:
CREATE TABLE IF NOT EXISTS `ci_sessions` (
        `id` varchar(40) NOT NULL,
        `ip_address` varchar(45) NOT NULL,
        `timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
        `data` blob NOT NULL,
        KEY `ci_sessions_timestamp` (`timestamp`)
);




Initializing a Session:
    To initialize the Session class manually in your controller constructor, use the meth
    $this->load- >library();
   $this->load->library('session');
Once loaded, the Sessions library object will be available using: $this->session;

Add Session Data: 

The same thing can be done in CodeIgniter as shown below.
$this->session->set_userdata('some_name', 'some_value');
set_userdata() function takes two arguments. The first argument,some_name, is the name of the session variable, under which, some_valuewill be stored.
set_userdata() function also supports another syntax in which you can pass array to store values as shown below.
$newdata = array( 
   'username'  => 'johndoe', 
   'email'     => 'johndoe@some-site.com', 
   'logged_in' => TRUE
);  
$this->session->set_userdata($newdata);
EXAMPLE INPUT:
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Session_input extends CI_Controller {
  public function index() {
                    //load the session library
                    $this->load->driver('session');
                    //load the view/session.php
                    $this->load->view('session_input');    
                    $this->load->helper('url');
                    //if usur submits the form, 
                    if ($this->input->post('userinput')) {
                               //save the input
                    $this->session->set_userdata('user_input', $this->input->post('userinput'));
                    //prompt the user that data has been save, give a link to session_output
          echo "<br>Session has been save! <a href='" . base_url() . "index.php/session_output'> Get the session from other page  </a>";
                    }         
          }
}

Retrieving Session Data:

After setting data in session, we can also retrieve that data as shown below.Userdata() function will be used for this purpose. This function will returnNULL if the data you are trying to access is not available.
$name = $this->session->userdata('name');
EXAMPLE OUTPUT:
<?php
if (!defined('BASEPATH'))
  exit('No direct script access allowed');
 
class Session_output extends CI_Controller {
 
          public function index() {
 
                    //load the session library
                    $this->load->driver('session');
 
                    //get the session
                    //if there is no session
                    if (!$this->session->userdata('user_input')) {
                               //prompt users that there is no session
                               $data["msg"] = "<b>No session!</b>";
                    } else {
                               //get the session[user_input]
                               $userinput = $this->session->userdata('user_input');
                               $data["msg"] = "User input say's: $userinput";
                    }
                    $this->load->view("session_output",$data);
          }
}
Session_view.php:
 
<!DOCTYPE html> <html lang = "en">
    <head> 
      <meta charset = "utf-8"> 
      <title>CodeIgniter Session Example</title> 
   </head>          
   <body> 
      Welcome <?php echo $this->session->userdata('name'); ?> 
      <br> 
      <a href = 'http://localhost/CodeIgniter-3.0.1/CodeIgniter3.0.1/index.php/sessionex/unset'>
         Click Here</a> to unset session data. 
   </body></html>






Destroying a Session:

   To clear the current session (for example, during a logout), you may simply use either PHP’s session_destroy() function, or thesess_destroy() method. Both will work in exactly the same way:
$this->session->sess_destroy();
//  or
$this->session->unset_userdata(‘some_name’); 







No comments:

Post a Comment