CodeIgniter Forums
What's better for my code below - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forum-1.html)
+--- Forum: Lounge (https://forum.codeigniter.com/forum-3.html)
+--- Thread: What's better for my code below (/thread-67424.html)



What's better for my code below - wolfgang1983 - 02-21-2017

I am just trying to figure out what is more appropriate these days.  What is better having my validate password function at top


PHP Code:
public function login($username$password) {
    if (!$this->validate_password($username$password)) {
        return FALSE;
    }
            
    $this
->db->select('ud.*, u.username');
    $this->db->from($this->db->dbprefix 'user u');
    $this->db->join($this->db->dbprefix 'user_data ud''ud.user_id = u.user_id');
    $this->db->where('status''1');
    $user_query $this->db->get();

    if ($user_query->num_rows() == 1) {

    $this->session->set_userdata('user_id'$user_query->row()->user_id);

    $this->user_id $user_query->row()->user_id;
    $this->username $user_query->row()->username;

    return TRUE;

    } else {
        return FALSE;
    }



Or like so or wrapped around


PHP Code:
public function login($username$password) {
    if ($this->validate_password($username$password)) {
            
        $this
->db->select('ud.*, u.username');
        $this->db->from($this->db->dbprefix 'user u');
        $this->db->join($this->db->dbprefix 'user_data ud''ud.user_id = u.user_id');
        $this->db->where('status''1');
        $user_query $this->db->get();

        if ($user_query->num_rows() == 1) {

        $this->session->set_userdata('user_id'$user_query->row()->user_id);

        $this->user_id $user_query->row()->user_id;
        $this->username $user_query->row()->username;

        return TRUE;

        } else {
            return FALSE;
        }

    } else {

        return FALSE;

    }




PHP Code:
public function validate_password($username$password) {
        $stored_password $this->getloginpassword($username);

        if (password_verify($password$stored_password)) {
            return TRUE;
        } else {
            return FALSE;
        }




RE: What's better for my code below - Wouter60 - 02-21-2017

I prefer the first solution. It prevents a lot of nesting if ... elseif... else... statements.
In your verificaton function, you can do this:
PHP Code:
return password_verify($password$stored_password); 
No need for an if .. else ...
The function itself already returns TRUE or FALSE.


RE: What's better for my code below - kidino - 03-15-2017

How about

PHP Code:
public function login($username$password) {
 
   if ($this->validate_password($username$password)) {
 
       $this->db->select('ud.*, u.username');
 
       $this->db->from($this->db->dbprefix 'user u');
 
       $this->db->join($this->db->dbprefix 'user_data ud''ud.user_id = u.user_id');
 
       $this->db->where('status''1');
 
       $user_query $this->db->get();

 
       if ($user_query->num_rows() == 1) {
 
           $this->session->set_userdata('user_id'$user_query->row()->user_id);
 
           $this->user_id $user_query->row()->user_id;
 
           $this->username $user_query->row()->username;
 
           return TRUE;
 
       
 
   }
 
   return FALSE;