password_hash()和password_verify()在php中的oops结构化代码中不起作用。实际上,它直接在执行其他部分(我的问题与过程类型无关,也许它是OOPS,而先前的问题都不与OOP相关)。
<?php
class security
{
function __construct()
{
$this->mysqli=getDBConn();
}
function login()
{
$mystat=$this->mysqli=getDBConn();
$username=$_POST["username"];
$password=$_POST["password"];
$escaped = $mystat->real_escape_string($mystat,$username);
$sql_query="SELECT *, u.id AS u_id, u.name AS u_name, ur.id AS ur_id, ur.name AS ur_name
FROM users u, user_roles ur WHERE username='".$username."' AND password='".$password."' AND
u.user_role_id=ur.id LIMIT 1";
$res=$this->mysqli->query($mystat,$sql_query);
if($res->mysqli_num_rows($res)){
$data_row=$res->fetch_assoc($res);
$_SESSION["logged_in"]=1;
$_SESSION["user"]=$data_row;
$password_hash=$data_row['password'];
if(password_verfiy($password,$password_hash)){
setMsg("You are logged in.", "success");
redirect("dashboard.php");
}
else
{
setMsg("Sorry, Invalid Credentials.", "danger");
redirect("index.php");
}
}
}
关于此代码,我在后端有两个表,一个是user_roles,第二个是users。