简体   繁体   中英

To store session and retrieve using codeigniter

In the below codeigniter code i placed model and view.My aim is to store college name in session and retrieved and used in other tables.Pls help me to do this. Model:

function validate()
    {
        $this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
            $this->db->where('college_name', $this->input->post('college_name'));
        $query = $this->db->get('membership');

        return $query;

    }

view:

<?php 
    echo form_open('login/validate_credentials');
    echo form_input('username', 'Username');
    echo form_password('password', 'Password');
        echo form_input('college_name', 'college_name');
    echo form_submit('submit', 'Login');
    echo anchor('login/signup', 'Create Account');
    echo form_close();
    ?>

To initialize the Session class manually in your controller constructor, use the $this->load->library function:

$this->load->library('session');

To add your data to the session array involves passing an array containing your new data to this function:

$this->session->set_userdata($array);

You can set array in session like :

$newdata = array(
                   'username'  => 'johndoe',
                   'email'     => 'johndoe@some-site.com',
                   'logged_in' => TRUE
               );

$this->session->set_userdata($newdata);

For further details please study this link .

To set data in session :

$this->load->library('session');
$this->session->set_userdata("collegeName",$collegeName);

To get data use :

$data = $this->session->userdata("collegeName");

For more info : ( http://ellislab.com/codeigniter%20/user-guide/libraries/sessions.html )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM