Welcome Guest, Not a member yet? Register   Sign In
Redirecting users- undefined index error
#1

[eluser]Corbee[/eluser]
I have a login form that directs people to the dashboard, but whenever there is no user yet, I kept getting undefined index error.

like this
Code:
Severity: Notice

Message: Undefined index: userid

Filename: controllers/welcome.php

Line Number: 13

What am I missing?

Controller
Code:
class Admin extends Controller {

    function Admin()
    {
        parent::Controller();    
        session_start();
    }
    
    function index()
    {
    if ($this->input->post('username'))
            {
                $u = $this->input->post('username');
                $pw = $this->input->post('password');
                $this->MAdmins->verifyUser($u,$pw);
                if ($_SESSION['userid']>0)
                    {
                        redirect('dashboard','refresh');
                    }
                else
                {
                    redirect('admin/index','refresh');    
                }
            }
        $data['main'] = 'login';
        $data['title'] = "Insurance Experts | Admin Login";
        $this->load->view('template',$data);
    }
}

Model
Code:
class MAdmins extends Model
{

    function MAdmins()
    {
    parent::Model();
    }
    
    function verifyUser($u,$pw)
    {
    $_SESSION['userid'] = NULL;
    $this->db->select('memberID,username');
    $this->db->where('username',$u);
    $this->db->where('Password',$pw);
    $this->db->where('memberID','2');
    $this->db->where('type','admin');
    $this->db->limit(1);
    $q = $this->db->get('member');
        if ($q->num_rows() > 0)
        {
            $row = $q->row_array();
            $_SESSION['userid'] = $row['memberID'];
            $_SESSION['username'] = $row['username'];
        }
        else
        {
            $this->session->set_flashdata('error','Sorry, your username or password is incorrect!');
        }
    
    }
    
}
#2

[eluser]cahva[/eluser]
You can't use $_SESSION['userid'](or any variable that hasnt been set) straight if you have not set it(and model doesnt set it if it doesnt find a match). Always check the variables like:
Code:
if (isset($_SESSION['userid']) && $_SESSION['userid']>0)

Although if you were using CI's own session lib(and I encourage you to do so) to your advantage, you would use it like this:
Code:
if ($this->session->userdata('userid') > 0)
#3

[eluser]Corbee[/eluser]
Thanks, it works!




Theme © iAndrew 2016 - Forum software by © MyBB