Welcome Guest, Not a member yet? Register   Sign In
What's better for my code below
#1

(This post was last modified: 02-21-2017, 03:04 AM by wolfgang1983.)

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;
        }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

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.
Reply
#3

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;

Reply




Theme © iAndrew 2016 - Forum software by © MyBB