PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 18, 2022

[FIXED] Login with hashed password in Codeigniter

 January 18, 2022     codeigniter, php     No comments   

Issue

I have store password with hashing in codeigniter. After that, I want to Login with hashed password. Secondly, i have to pass the user's id.. i have take help of this website but it lands on error page. i have used password_verify(). but it gives long error

what i have tried model

 public function isvalidateuser($email,$password)
    {
        $q = $this->db->where(['email'=>$email,'password'=>$password])
                    ->get('user');
                    $row = $q->row();

                    if(password_verify($password,$row->password))
                    {
                       
                        return $q->row()->id;
                      
                    }

                    else
                    {
                        return false;
                    }
    }

controller

public function  userloginvalidation1()
{
    $email    = $this->input->post('email');
    $password = $this->input->post('password');

    $this->load->model('loginmodel');
    
    $id = $this->loginmodel->isvalidateuser($email, $password);
    
    if ($id) {
   
        // You probably want to do this in model ↓↓
        $userName = $this->db->select('name')->from('user')->where('id', $id)->get()->row(); // get the name from the table

        $this->load->library('session'); // autoloading the session library is a good idea
        
        // set the session 
        $this->session->set_userdata('id', $id);
        $this->session->set_userdata('name', $userName->name); // or simply $_SESSION['name'] = $userName->name;
        
        return redirect('user/userdashboard');
    
    } else {
        
        echo "not login;";
        exit();
        $this->load->library('session');
        $this->session->set_flashdata('loginfailed', 'invalid username or password');
     
        return redirect('user/userlogin');
    }      
}

the error ihave got

A PHP Error was encountered
Severity: Notice

Message: Trying to get property 'password' of non-object

Filename: models/loginmodel.php

Line Number: 16

Backtrace:

File: C:\xampp\htdocs\blog\application\models\loginmodel.php
Line: 16
Function: _error_handler

File: C:\xampp\htdocs\blog\application\controllers\user.php
Line: 45
Function: isvalidateuser

File: C:\xampp\htdocs\blog\index.php
Line: 315
Function: require_once

not login;

how to solve this error???? help!!!


Solution

Your problem is with code in model

$q = $this->db->where(['email'=>$email,'password'=>$password])
                    ->get('user');
                    $row = $q->row();

You are querying the database table on field email and password. I assume password field store hash value and you are querying with raw value. The query will never return any data.

So you should do as follow

$q = $this->db->where(['email'=>$email)
                    ->get('user');
                    $row = $q->row();

Now you should have hashed password from table to compare.

Still you code have an issue and will throw same error when it can't find any data where email is missing. To avoid that you can

if($q->num_rows()>0){
   // your Code
}


Answered By - mail2bapi
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing