As I am new to code igniter and MVC, I am slowly but surely getting my login form to log a user in via data I’ve stored in the db.
I have pieced together my current version from articles and advice from forums, however I am a little confused at this point as to why it seems that data is not being stored and sent properly.
I have my login form set up to re-load the form if the password is incorrect ( eventually will add a message)
That being said, when I put in the proper login info it reloads the page. That can only tell me that I am not communicating with the db properly or not sending and or storing info with my $data variable properly.
Here is what I have code wise:
Model:
<?php
class User_model extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function login($data = array())
{
// validate data
if( !empty($data) ) return FALSE;
// retrieve query
$query = $this->db
->from('users')
->where($data)
->get();
// Check if query row exists
if($query->row())
{
// Query row exists, return query row
return $query->row();
}
else
{
// Query row doesn't exist, return FALSE
return FALSE;
}
}
}
View:
title>Login</title>
<!--MAKE SURE SIGNED OUT HEADER IS IMPLEMENTED FOR ALL SIGNED OUT PAGES INCLUDING THIS ONE-->
<div class="structure clearfix">
<h1 class="title_header">
Sign In
</h1>
<div id="signin_form">
<?php
echo validation_errors();
echo form_open('auth/validate_credentials');
echo "<div class='form_text_signin'>";
echo "Email";
echo "</div>";
echo form_input('email');
echo "<div class='form_text_signin'>";
echo "Password";
echo "</div>";
echo form_input('password');
echo form_submit('submit', 'Submit');
echo form_close();
?>
</div>
</div>
Controller:
<?php
class Auth extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
// this is automatically called if no other function is called
// it simply turns around and calls the login() function to show the login page
public function index() {
$this->login();
}
public function login() {
$data['main_content'] = 'auth/login';
$this->load->view('includes/templates/main_page_template', $data);
}
function validate_credentials () {
$query = $this->load->model('user_model');
if($query)
{
$data = array(
'email' => $this->input->post('email'),
'password' => $this->input->post('password'),
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('account/dashboard');
}
else
{
$this->index();
}
}
}
thanks in advance.
It doesn’t look like you’re calling the login method from your user_model class at any point – you’re just instantiating the class. You should be calling the model method from your controller – I haven’t tested this code but hopefully it gets you on the right track: