• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's better for my code below

#1
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


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2017 MyBB Group.