我正在开发登录模块,我有两个场景:
。
对于第一个场景,我创建了一个名为is_first_login的列,其值为0。重置密码后,它将从0更改为1
现在我的问题是,当我插入虚拟密码时,它就卡在process.php页面上了。
我的密码里有遗漏什么吗?
function login($pdo) {
if (!filter_var($_POST['username'] ?? '', FILTER_VALIDATE_EMAIL)) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Please enter the valid email id</label>';
header('Location: index.php?id=1');
exit();
} elseif (empty($_POST['password'])) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Please enter the valid password</label>';
header('Location: index.php?id=1');
exit();
}
$sql = "SELECT company_id,company_email,password,is_first_login FROM company21 WHERE company_email=:username AND password=:password and is_active=1";
$stmt = $pdo->prepare($sql);
$stmt->bindParam('username', $_POST['username']);
$stmt->bindParam('password', $_POST['password']);
$stmt->execute();
if (!$stmt->rowCount()) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Email & Password does not match1</label>';
header('Location: index.php?id=1');
exit();
}
$result = $stmt->fetch(PDO::FETCH_ASSOC);
//print_r($result);
if (!empty($result['is_first_login']) && $result['is_first_login'] == 0) {
header("Location:reset-password.php");
exit();
}
if (!empty($result['is_first_login']) && $result['is_first_login'] == 1) {
// echo "working1";
if (!password_verify($_POST['password'], $result['password'])) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Email & Password does not match2</label>';
header('Location: index.php?id=1');
exit();
} else {
$_SESSION['company_id'] = $result['company_id'];
$_SESSION['company_email'] = $result['company_email'];
header("Location:edit-company.php");
exit();
}
}
}如果第一次用户登录,我将得到结果中的输出。
Array ( [company_id] => 2 [company_email] => abc@gmail.com [password] => 5JXB6W [is_first_login] => 0 ) 发布于 2021-05-25 04:05:46
我不知道这是不是正确的方式,但这段代码已经解决了我的问题。我愿意接受更好的解决办法。
我所做的:我又在数据库中创建了一个名为"temp_password"的列,并在其中添加了我所有的虚拟密码。
注意:现在我有两个列作为密码,一个列用于哈希密码,第二个列用于虚拟密码。
现在,当用户登录时,我首先检查密码是散列密码。如果散列,则重定向到编辑页(如果匹配)。
如果密码不是散列,那么我将密码与temp_password进行比较。如果匹配,则重定向到重置页,如果不匹配,则显示错误消息"Email & Password does not match"。
function login($pdo) {
if (!filter_var($_POST['username'] ?? '', FILTER_VALIDATE_EMAIL)) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Please enter the valid email id</label>';
header('Location: index.php?id=1');
exit();
} elseif (empty($_POST['password'])) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Please enter the valid password</label>';
header('Location: index.php?id=1');
exit();
}
$sql = "SELECT company_id,company_email,password,is_first_login,temp_password FROM company21 WHERE company_email=:username and is_active=1";
$stmt = $pdo->prepare($sql);
$stmt->bindParam('username', $_POST['username']);
$stmt->execute();
$result = $stmt->fetch(PDO::FETCH_ASSOC);
if (!password_verify($_POST['password'], $result['password'])) {
if ($_POST['password']==$result['temp_password']) {
header('Location: reset-password.php');
exit();
}
else{
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Email & Password does not match</label>';
header('Location: index.php?id=1');
exit();
}
}
else{
$_SESSION['company_id'] = $result['company_id'];
$_SESSION['company_email'] = $result['company_email'];
header("Location:edit.php");
exit();
}
}发布于 2021-05-24 06:36:58
由于您已经使用了空()函数,如果以下值计算为空,它将返回true:
0
0.0
"0"
""
NULL
FALSE
array()所以,改变它,使条件发挥作用。
更改代码:-
function login($pdo) {
if (!filter_var($_POST['username'] ?? '', FILTER_VALIDATE_EMAIL)) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Please enter the valid email id</label>';
header('Location: index.php?id=1');
exit();
} elseif (empty($_POST['password'])) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Please enter the valid password</label>';
header('Location: index.php?id=1');
exit();
}
$sql = "SELECT company_id,company_email,password,is_first_login FROM company21 WHERE company_email=:username AND password=:password and is_active=1";
$stmt = $pdo->prepare($sql);
$stmt->bindParam('username', $_POST['username']);
$stmt->bindParam('password', $_POST['password']);
$stmt->execute();
if (!$stmt->rowCount()) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Email & Password does not match1</label>';
header('Location: index.php?id=1');
exit();
}
$result = $stmt->fetch(PDO::FETCH_ASSOC);
//print_r($result);
if ($result['is_first_login']==0) {
header("Location:reset-password.php");
exit();
}
if($result['is_first_login']==1) {
// echo "working1";
if (!password_verify($_POST['password'], $result['password'])) {
$_SESSION['error'] = '<label class="text-danger fs-6 pt-2">Email & Password does not match2</label>';
header('Location: index.php?id=1');
exit();
}
else{
$_SESSION['company_id'] = $result['company_id'];
$_SESSION['company_email'] = $result['company_email'];
header("Location:edit-company.php");
exit();
}
}
}https://stackoverflow.com/questions/67667464
复制相似问题