CodeIgniter Forums
Login Validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Login Validation (/showthread.php?tid=6654)



Login Validation - El Forum - 03-06-2008

[eluser]SparkyCola[/eluser]
Hi,

I'm a bit stuck. In PHP, this is what I want to do, just as a rough idea:

Code:
$query = "SELECT * FROM users WHERE username='$user_name' AND password='$password'";
$result = mysql_query($query, $connection) or die ('error making query');
$affected_rows = mysql_num_rows($result);
if($affected_rows == 1) {
  // Validated so continue...
} else {
  // Not validated so error
}



In CI this is what I have:

Code:
function _validation($user_name, $password) {
        
        $rules['username']    = "required|callback_username_check";
        $rules['password']    = "required|";

        $this->validation->set_rules($rules);
    }

    function username_check($user_name) {
        
        $query = $this->db->query("SELECT * FROM users WHERE username = '$user_name' LIMIT 1");
        $row = $query->row_array();
        
        if ($query->num_rows() == 0) {
            $this->validation->set_message('username_check', 'Not found in the user database');
            return FALSE;
        } else {
            $db_username = $row['username'];
            
            if ($user_name == $db_username) {
                return TRUE;
            } else {
                $this->validation->set_message('username_check', 'Not found in the user database');
                return FALSE;
            }
        }
    }

I'd appreciate any help, just not sure how to achieve the same result - i.e. checking the username AND password together. I could do a callback thing with password as well, but I don't want to do that in case the username is in the database, and the password is in the DB too, but not together on the same row. Thanks,

Sparky


Login Validation - El Forum - 03-06-2008

[eluser]James Gifford[/eluser]
There are any number of ways to do validation that depend a lot on your preferred coding style. Here is a simple way:
Code:
function login ()
{
   $this->load->library('validation');

   $fields['user_name'] = 'username';
   $fields['password'] = 'password';

   $this->validation->set_fields($fields);

   $rules['user_name'] = 'required|callback__check_login';
   $rules['password'] = 'required';

   $this->validation->set_rules($rules);

   if ($this->validation->run())
   {
      // Log user in
   }

   // Display the login form
}

function _check_login ($user_name)
{
   $this->validation->set_message('_check_login', 'Your login information is invalid');

   $this->db->where('user_name', $user_name);
   $this->db->where('password', $this->validation->password);
   $query = $this->db->get('users');

   return ($query->num_rows() > 0);
}



Login Validation - El Forum - 03-06-2008

[eluser]SparkyCola[/eluser]
Ah I see- thanks very much James Smile

Sparky