Welcome Guest, Not a member yet? Register   Sign In
My login page is not working
#1

(This post was last modified: 04-28-2021, 10:22 AM by velismo.)

Hi, there!

I'm trying to create a login page and a registration page. The registration works perfectly, or I believe so, but the login not. When I introduce the username and the password, it brings me an error login message "login invalid" and I don't know why. I saw you here my code:

CONTROLLER (Pages.php)

PHP Code:
public function login(){
            
$data['title'] = 'Sign In';

            
$this->form_validation->set_rules('username''Username''required');
            
$this->form_validation->set_rules('password''Password''required');

            if(
$this->form_validation->run() === FALSE){
                
$this->load->view('templates/header');
                
$this->load->view('users/login'$data);
                
$this->load->view('templates/footer');
            } else {
                
                
// Get username
                
$username $this->input->post('username');
                
// Get and encrypt the password
                
$password md5($this->input->post('password'));

                
// Login user
                
$user_id $this->user_model->login($username$password);

                if(
$user_id){
                    
// Create session
                    
$user_data = array(
                        
'user_id' => $user_id,
                        
'username' => $username,
                        
'logged_in' => true
                    
);

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

                    
// Set message
                    
$this->session->set_flashdata('user_loggedin''You are now logged in');

                    
redirect('servicioscolegiado');
                } else {
                    
// Set message
                    
$this->session->set_flashdata('login_failed''Login is invalid');

                    
redirect('users/login');
                }        
            }
        } 

MODEL(User_model.php)
PHP Code:
public function login($username$password){
            
// Validate
            
$this->db->where('username'$username);
            
$this->db->where('password'$password);

            
$result $this->db->get('users');

            if(
$result->num_rows() == 1){
                return 
$result->row(0)->id;
            } else {
                return 
false;
            }
        } 


VIEW(login.php)

PHP Code:
    <?php echo form_open('users/login'); ?>
        <div class="row">
            <div class="col-md-4 col-md-offset-4">
                <div class="form-group">
                    <input type="text" name="username" class="form-control" placeholder="Enter Username" required autofocus>
                </div>
                <div class="form-group">
                    <input type="password" name="password" class="form-control" placeholder="Enter Password" required autofocus>
                </div>
                <button type="submit" class="btn btn-primary btn-block">Login</button>
            </div>
        </div>
    <?php echo form_close(); ?>

I'm seriously mad right now because I cannot see the problem and it's starting to be a little bit frustrating. Please any guide will be more than welcome.

Thank you so much and have a great day.
Reply
#2

For one you should not be using MD5 it is a security waiting to happen.

Use the PHP.net - password_hash and password_verify
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

The question level is why two plus two does not equal five.
This is a forum thread for CodeIgniter4. Find the problem yourself.
Reply
#4

(04-28-2021, 10:34 PM)iRedds Wrote: The question level is why two plus two does not equal five.
This is a forum thread for CodeIgniter4. Find the problem yourself.

Why you waste your time giving that kind of answer?. I've found the problem. What I'm trying to get now is the solution. Anyway, have a good day. You will need it.
Reply
#5

Check your model, part:

PHP Code:
if($result->num_rows() == 1){
                return $result->row(0)->id;
            } else... 

to try it, just get a row as return without if($result->num_rows() etc).
An example

PHP Code:
public function check_login($username$password){
        $db      = \Config\Database::connect();
        $builder $db->table('users_db');

        $query $builder->select('*')
                         ->where('username'$username)
                         ->where('password'$password)
                         ->get();

        return $query->getRow();
    

On your controller, just query:

PHP Code:
if($check_login) {
  
start your session here etc.



Btw, MD5 is not so secure. Take InsiteFX's suggestion into consideration
Reply




Theme © iAndrew 2016 - Forum software by © MyBB