0

i am using password_hash() and password_verify() for hashing passwords but passwords_verify() returns false.This is my code

<?php
/**
 * .
 *
 * @author Anmol Raghuvanshi <ershadow786@gmail.com>
 * @license
 * @copyright
 */

class User_admin
{

  private $db;

    function __construct($DB_con)
    {
      $this->db = $DB_con;
    }

    public function register($fname,$lname,$umail,$upass,$mobile,$address)
    {
       var_dump($upass);
       try
       {
           $new_password = password_hash($upass, PASSWORD_DEFAULT);
           var_dump($new_password);
           $stmt = $this->db->prepare("INSERT INTO gy_registration(name,mobile_no,address,email_address)
                                                       VALUES(:fname,:umail,:mobile_no,:address)");

           $stmt->bindparam(":fname", $fname);
           $stmt->bindparam(":umail", $umail);
           $stmt->bindparam(":mobile_no", $mobile);
           $stmt->bindparam(":address", $address);
           $stmt->execute();

           $query=$this->db->prepare("INSERT INTO gy_user_detail(reg_id,first_name,last_name,username,password)"
                   . " VALUES (last_insert_id(),:fname,:lname,:umail,:upass)");
           $query->bindparam(":fname", $fname);
           $query->bindparam(":lname", $lname);
           $query->bindparam(":umail", $umail);
           $query->bindparam(":upass", $new_password);
           $query->execute();
           return $stmt;
       }
       catch(PDOException $e)
       {
           echo $e->getMessage();
       }
    }


    public function login($umail,$upass)
    {
    var_dump($upass);
       try
       {    
          $stmt = $this->db->prepare("SELECT * FROM gy_user_detail WHERE username=:umail LIMIT 1");
          $stmt->execute(array(':umail'=>$umail));
          $userRow=$stmt->setFetchMode(PDO::FETCH_ASSOC);
          var_dump(password_verify($upass,$userRow['password']));
          if($stmt->rowCount() > 0)
          { 
              if(password_verify($upass, $userRow['password'])) {
        echo "verified";
                //$_SESSION['user_session'] = $userRow['user_id'];
                return true;
          }
            else
             {

                return false;
             }
          }
       }
       catch(PDOException $e)
       {
           echo $e->getMessage();
       }
   }

   public function is_loggedin()
   {
    echo "h";
    //  if(isset($_SESSION['user_session']))
    //  {
      //   return true;
    //  }
   }

   public function redirect($url)
   {
       header("Location: $url");
   }

   public function logout()
   {
  //      session_destroy();
    //    unset($_SESSION['user_session']);
        return true;
   }
}

?>

i have var_dump password before and after hashing value .hash value is stored in database but when it comes to verify() it fails.any help??
i have passsowrd field in database as 255 varchar

1
Contributor
2
Replies
25
Views
1 Year
Discussion Span
Last Post by shany0786
0

replacing $userRow=$stmt->setFetchMode(PDO::FETCH_ASSOC); in login function with fetch() methods works for me but not with $userRow=$stmt->setFetchMode(PDO::FETCH_ASSOC) can anybody explain this

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.