Professional CodeIgniter

(singke) #1

Chapter 6: Creating a Dashboard


151


$this- > session- > set_flashdata(‘error’, Sorry, your username or password is
incorrect!’);
}
}

}

Notice that the verifyUser() function stores an error message in CodeIgniter ’ s flash data if there is no
match on the query. You ’ ll use these flash data to display the error in case the user mistypes her
username or password.


Don ’ t forget to add MAdmins to the list of models that get autoloaded! Simply edit the list in /system/
application/config/autoload.php.


$autoload[‘model’] = array(‘MCats’,’MProducts’,’MOrders’,’MAdmins’);

Now that you have the table built and the corresponding model hooked up to it, it ’ s time to create a
basic verify() function in the Welcome controller.

You use the verify() function to do several things:


  1. First, you check to see if there are any incoming POST data.

  2. If there are incoming POST data, take the username and password field information and send it
    to the verifyUser() function of the MAdmins model.

  3. Once that process has run, check to see if there is a value in the userID session variable that is
    greater than 0. If so, redirect the user to the admin/dashboard controller.

  4. Otherwise, show the login view.


Here ’ s the new controller function:

function verify(){
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(‘admin/dashboard’,’refresh’);
}
}
$data[‘main’] = ‘login’;
$data[‘title’] = “Claudia’s Kids | Admin Login”;
$data[‘navlist’] = $this- > MCats- > getCategoriesNav();
$this- > load- > vars($data);
$this- > load- > view(‘template’,$data);
}

Next comes the login view. This will be just a simple form that prompts the user for a username and
password and then posts to welcome/verify, where the entered credentials can be verified.
Free download pdf