Sunday, January 30, 2022

[FIXED] how to use session in login system and implement logout in codeigniter

Issue

New in codeigniter learning it and stuck on the session for codeigniter. I am working on a login system the login part is working good but now i have to add session and logout and include my other pages in to that session so i can restrict someone to direct access that page. so how i implement session & logout in my code.

This is the Controller

<?php
class LoginController extends CI_controller
        {
            public function index()
        {
            $this->load->view('header2');
            $this->load->view('login');
            $this->load->view('footer');
        }
            public function checklogin()
        {
            $this->form_validation->set_rules('username' ,'Username', 'required|valid_email');
            $this->form_validation->set_rules('password' ,'Password', 'required|callback_verifyUser');

                if($this->form_validation->run() == false){
                    $this->load->view('header2');
                    $this->load->view('login');
                    $this->load->view('footer');
                    }
                    else
                    {
                        redirect('HomeController/index');
                    }
                }
                    public function verifyUser()
                    {
                        $name = $this->input->post('username');
                        $pass = $this->input->post('password');

                        $this->load->model('LoginModel');

                        if($this->LoginModel->login($name, $pass))
                        {
                            return true;
                        }
                        else
                        {
                            $this->form_validation->set_message('verifyUser','Incorrect Email or Pass');
                            return false;
                        }
                        redirect('LoginController/checklogin');
                    }
            }

The model is like this

<?php
        class LoginModel extends CI_model
        {
            public function login($name, $pass)
            {
                $this->db->select('name,pass');
                $this->db->from('members');
                $this->db->where('name',$name);
                $this->db->where('pass',$pass);     

                $query = $this->db->get();

                if($query->num_rows() == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

The View is

                  <html>
                    <head>
                    <title></title>
                </head>
                <body>
                <?php echo validation_errors(); ?>
                <?php echo form_open('LoginController/checklogin'); ?>
                UserName: 
                <input type="text" name="username" /><br/>
                password:
                <input type="text" name="password" />
                <input type="submit" value="Login" name="submit" />
                </form>
                </body>
                </html>

login is working correctly need a session and logout in this.


Solution

1) First Include the Library:

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

2) Store the data in array which you want to store in session :

$data= array(
                   'username'  => 'username',
                   'password'     => 'password',
                   'email'=>'emailid'
                   'logged_in' => 'login id'
               );

3) Store this data in session:

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

You can use this data any time for example if you want loginid from session you can write:

$loginid= $this->session->userdata('logged_in');

When LOGOUT :

$this->session->unset_userdata(array("username"=>"","logged_in"=>"","password"=>"","email"=>""));
$this->session->sess_destroy();
redirect('loginpage');

If you don't want to direct access the pages create one model and add this code in it and auto load that model.

public function valid_allowed()//check user is login or not
    {
        $session = $this->session->userdata('username'); //here you can take loginid, email whatever you store in session
        if(!$session)
        {
            redirect('login');
        }
    }

To auto load that model

Go to application->config->autoload.php: this line:

$autoload['model'] = array('model name');

Change you model:

<?php
        class LoginModel extends CI_model
        {
            public function login($name, $pass)
            {
                $this->db->select('name,pass');
                $this->db->from('members');
                $this->db->where('name',$name);
                $this->db->where('pass',$pass);     

                $query = $this->db->get();

                if($query->num_rows() == 1)
                {
                    $row=$query->row();
                    $data=array(
                    'username'=>$row->u_username,
                    'id'=>$row->id,
                    'email'=>$row->email
                    'password'=>$row->u_password);
                    $this->session->set_userdata($data);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

Change your verifyUser function:

 public function verifyUser()
                        {
                            $name = $this->input->post('username');
                            $pass = $this->input->post('password');

                            $this->load->model('LoginModel');
                            $result=$this->LoginModel->login($name, $pass))
                         if(!$result)                   
                         {

             $this->form_validation->set_message('verifyUser','Incorrect Email or Pass');
                                return false;
                     redirect('LoginController/checklogin');

                        }
                        else
                        {   
                           redirect('dashboard');   //user is valid so goto dashboard
                        }
}


Answered By - David Coder

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.