Invalid Username or Password in Codeigniter 3

user5913892

I have followed a tutorial in order to create a simple Login with CodeIgniter(the version I'm using is the last one), but when I insert the Username and Password I just get "Invalid Username or Password" that would be great if I missed one of the two, but both are correct. The DB was created in PHPmyAdmin(I'm using WAMP with user root and no password), the table is named users and I created two different users. If necessary I can also put the code of other files ( database.php , etc. ). SO,I don't understand What I'm wrong, this is the controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class VerifyLogin extends CI_Controller {

     function __construct()
     {
       parent::__construct();
       $this->load->model('user','',TRUE);
       $this->load->helper('url');
     }

     function index()
     {
       //This method will have the credentials validation
       $this->load->library('form_validation');

       $this->form_validation->set_rules('username', 'Username', 'trim|required');
       $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');

       if($this->form_validation->run() == FALSE)
       {
         //Field validation failed.  User redirected to login page
         $this->load->view('login_view');
       }
       else
       {
         //Go to private area
         redirect('home', 'refresh');
       }

     }

     function check_database($password)
     {
       //Field validation succeeded.  Validate against database
       $username = $this->input->post('username');

       //query the database
       $result = $this->user->login($username, $password);

       if($result)
       {
         $sess_array = array();
         foreach($result as $row)
         {
           $sess_array = array(
             'id' => $row->id,
             'username' => $row->username
           );
           $this->session->set_userdata('logged_in', $sess_array);
         }
         return TRUE;
       }
       else
       {
         $this->form_validation->set_message('check_database', 'Invalid username or password');
         return false;
       }
     }
    }
    ?>

This is User.php

<?php
Class User extends CI_Model
{
 function login($username, $password)
 {
   $this -> db -> select('id, username, password');
   $this -> db -> from('users');
   $this -> db -> where('username', $username);
   $this -> db -> where('password', MD5($password));
   $this -> db -> limit(1);

   $query = $this -> db -> get();

   if($query -> num_rows() == 1)
   {
     return $query->result();
   }
   else
   {
     return false;
   }
 }
}
?>
devpro

As per your comments:

Yes,It returns this: Array ( [0] => stdClass Object ( [id] => 1 [username] => fabio [password] => fabio ) ) . Sorry, before I deleted that print , I did not understand what you wanted to do. – user5913892 9 mins ago

You do not have password in MD5() in your database. for this you need to use this:

$this -> db -> select('id, username, password');
$this -> db -> from('users');
$this -> db -> where('username', $username);
$this -> db -> where('password', $password);
$this -> db -> limit(1);

It is good practice to use encryption for your confidential data like password, i suggest you to INSERT password as MD5() than you can use MD5() in login.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

GitHub: invalid username or password

From Java

Sourcetree remote: Invalid username or password

From Dev

PHP login "invalid username and password"

From Dev

orientdb connect invalid password and username

From Dev

Codeigniter Login Validating Incorrect Username & Password

From Dev

Codeigniter Login Validating Incorrect Username & Password

From Dev

Check if username and password matched using ajax codeigniter

From Dev

C# Error 401 - Invalid username or password

From Dev

Domino Web SSO : Invalid username or password was specified

From Dev

Google app engine launcher: Invalid username or password

From Dev

Cannot deploy with goapp - invalid username or password

From Dev

remote: Invalid username or password, fatal: Authentication failed for

From Dev

Invalid username and password on login, CakePHP 2.5.5

From Dev

Username/Password Invalid Login Android Apps

From Dev

Php login "Invalid username and password" using mysql

From Java

git remote: Invalid username or password. fatal: Authentication failed - ubuntu

From Dev

remote: Invalid username or password. fatal: Authentication failed for

From Dev

kuali - ORA-01017: invalid username/password; logon denied

From Dev

Invalid username or password at login() using BlowfishPasswordHasher in CakePHP 2.x

From Dev

New One or both of Username and Password are invalid. Error

From Dev

How to resolve unable to push to GitHub due to "Invalid username or password"?

From Dev

spring security bad credentials distinguish between invalid username or password

From Dev

How to throw an error for Invalid username or password if the data is not matched with the DB?

From Dev

Display Error message when connection or Invalid Username or Password in VBA

From Dev

Codeigniter 3 session with redis - AUTH password

From Dev

Spring security authentication with 3 fields instead of just username and password

From Dev

Should I use sqlite3 for storing username and password with python?

From Dev

IdentityServer3 get user token by username and password post REQUEST

From Dev

Spring security authentication with 3 fields instead of just username and password

Related Related

  1. 1

    GitHub: invalid username or password

  2. 2

    Sourcetree remote: Invalid username or password

  3. 3

    PHP login "invalid username and password"

  4. 4

    orientdb connect invalid password and username

  5. 5

    Codeigniter Login Validating Incorrect Username & Password

  6. 6

    Codeigniter Login Validating Incorrect Username & Password

  7. 7

    Check if username and password matched using ajax codeigniter

  8. 8

    C# Error 401 - Invalid username or password

  9. 9

    Domino Web SSO : Invalid username or password was specified

  10. 10

    Google app engine launcher: Invalid username or password

  11. 11

    Cannot deploy with goapp - invalid username or password

  12. 12

    remote: Invalid username or password, fatal: Authentication failed for

  13. 13

    Invalid username and password on login, CakePHP 2.5.5

  14. 14

    Username/Password Invalid Login Android Apps

  15. 15

    Php login "Invalid username and password" using mysql

  16. 16

    git remote: Invalid username or password. fatal: Authentication failed - ubuntu

  17. 17

    remote: Invalid username or password. fatal: Authentication failed for

  18. 18

    kuali - ORA-01017: invalid username/password; logon denied

  19. 19

    Invalid username or password at login() using BlowfishPasswordHasher in CakePHP 2.x

  20. 20

    New One or both of Username and Password are invalid. Error

  21. 21

    How to resolve unable to push to GitHub due to "Invalid username or password"?

  22. 22

    spring security bad credentials distinguish between invalid username or password

  23. 23

    How to throw an error for Invalid username or password if the data is not matched with the DB?

  24. 24

    Display Error message when connection or Invalid Username or Password in VBA

  25. 25

    Codeigniter 3 session with redis - AUTH password

  26. 26

    Spring security authentication with 3 fields instead of just username and password

  27. 27

    Should I use sqlite3 for storing username and password with python?

  28. 28

    IdentityServer3 get user token by username and password post REQUEST

  29. 29

    Spring security authentication with 3 fields instead of just username and password

HotTag

Archive