Apply for Zend Framework Certification Training

Codeigniter




    In this Blog We will see Session class in CodeIgniter (CI). 
    ---------------------------------------------------------------------------

The Session class is responsible for initializing session cookies, 
setting session data, and returning session data when it’s called in 
your application. We’ll be creating the session, creating session data, 
and then reading session data from our session cookie.

Step 1 Session Configuration
   ------------------------------
           First we have to initialize a session cookie in autoload.php 
           $autoload['libraries'] = array('session');

           Now our session cookie is autoloaded globally across the application.

Step 2  session cookies to be  encrypted. 
    -------------------------------------------
    Navigate to config.php and change the following to TRUE 
            $config['sess_encrypt_cookie']    = TRUE;

< ?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class login extends CI_Controller {
     
    public function index()
    {
        $data['id'] = $this->session->userdata('session_id');
        $this->load->view('data', $data);
    }
}
    The View file
-----------------------
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
    <p> Session ID: <?php echo $id ?> </p>
</body>
</html>

   Adding Session Data
--------------------------------
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class login extends CI_Controller {
 
    public function index()
    {
        $sessionData = array( 'username' => 'user');
        $this->session->set_userdata($sessionData);
        $data['id'] = $this->session->userdata('session_id');
        $data['username'] = $this->session->userdata('username');
        $this->load->view('data', $data);
    }
}
You can see we’re passing a username into the $sessionData array 
named user, then we assign our session ‘username’ to our $data array
 with the same ‘username’ key.Now that we’re passing the username 
into our view through the $data array, we need to display it in the view,
so go into data.php and write the following:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>
    <p> Session ID: < ?php echo $id ?> </p>
    <p> Username: < ?php echo $username ?> </p>
</body>
</html>
Now when you open your application in the browser you’ll see your session id and the username user.

Note :- 
--------
If you want to see all the session value 

print_r($this->session->all_userdata()); exit();                                    

< Zendframework2 Insert Using Ajax Hooks in Codeigniter >



Ask a question



  • Question:
    {{questionlistdata.blog_question_description}}
    • Answer:
      {{answer.blog_answer_description  }}
    Replay to Question


Back to Top