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

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




Theme © iAndrew 2016 - Forum software by © MyBB