JSSR JSSR - 6 months ago 69
SQL Question

Codeigniter 3.0.6 Login not working

I am new to CodeIgniter. I am going to build a very simple register/login system for the purpose of testing, but I've never got logged in by the registered email and password.

Here is my Login_controller:

<?php

class Login_controller extends CI_Controller {

function __construct() {
parent::__construct();
}

public function index($msg = NULL) {
$data['msg'] = $msg;
$this->load->view('login_view', $data);
}

public function process() {
// Load the model
$this->load->model('login_model');
// Validate the user can login
$result = $this->login_model->validate();
// Now we verify the result
if(! $result) {
// If not valid user, then show them login page again
$msg = 'Invalid email and/or password';
$this->index($msg);
}
else {
// If valid user, go to homepage
redirect('home_view');
}
}
}


My Login_model:


class Login_model extends CI_Model {
function __construct() {
parent::__construct();
}

public function validate() {
// Get user input
$email = $this->input->post('email');
$password = $this->input->post('password');

// Prepare the query
$this->db->select();
$this->db->from('user');
$this->db->where('email', $email);
$this->db->where('password', $password);

// Run the query
$query = $this->db->get();

// Check result
if($query->num_rows > 0) {
// If there is a user then create session data
$row = $query->row();
$data = array(
'id' => $row->id,
'username' => $row->username,
'email' => $row->email,
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
else {
// If the previous process did not validate then return false
return false;
}
}
}


And my Login_view:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</head>
<body>
<h1> Login Zokzak </h1>

<?php
echo form_open('Login_controller/process');
?>

Email: <input type="email" name="email"/> <br> <br>
Password: <input type="password" name="password"/> <br>
<input type="submit" value="Login"/>

<?php
if(!is_null($msg)) {
echo $msg;
}
?>
</body>
</html>


Thank you in advance!

Answer

First, you have to make difference between loading view and redirecting page. If home_view is APPPATH.'views' directory's file redirect('home_view'); won't work. Redirect function accepts REQUEST_URI string - in example redirect('controller_name/method_name/param1/param2'). View file is loaded by CI loader. For creating message you need to use flash session that will be available on next request.

if(! $result) {
    // If not valid user, then show them login page again
    $this->session->set_flashdata('msg', 'Invalid email and/or password');
    redirect('login_controller');//index method is loaded by default
}
else
{
    $this->session->set_flashdata('msg', 'Successfully Logged In.');
    redirect('top_secret_controller/admin_dashboard_method');
}

In Top_secret_controller.php:

class Top_secret_controller extends CI_Controller
{
    private $logged_in = FALSE;

    public function __construct()
    {
        parent::__construct();
        $this->logged_in = ( isset($_SESSION['validated']) && $_SESSION['validated'] === 1 ) ? TRUE : $this->logged_in;//in your model use integer for validated value instead TRUE to [avoid confusing](https://dev.mysql.com/doc/refman/5.7/en/boolean-literals.html) with converting to integer since in DB it will be writen as 1

        $this->logged_in || redirected('login');//if not logged in redirect to login page
    }

    public admin_dashboard_method()
    {
        //code here
    }
}

Quick YT search will give you bunch of videos (like this one) about login system so you could compare good practices with your own. Personally, I am using Ion Auth system not wanting inventing the wheel (unless I need one that is more rounder :P ).