我正在使用学校的登录表格。我无法登录,因为如果我提交var_dump($ password),则每次提交的字符串都是空的。我不知道我在做什么错。
<?php
session_start();
function canLogin($email,$password){
$conn = new mysqli("localhost","root","","buddy_app");
$email = mysqli_real_escape_string($conn,$email);
$password = mysqli_real_escape_string($conn,$password);
$sql = "select password from tl_user where email='$email'";
$result = $conn->query($sql);
if($result->num_rows != 1){
return false;
}
var_dump($password);
$user = $result->fetch_assoc(); //fetch=pakken
$hash = $user['password'];
if(password_verify($password, $hash)){
return true;
}else{
return false;
}
}
// if form was submit
if(!empty($_POST) ) {
$email = $_POST ['email'];
$password = $_POST ['password'];
// check if required fields are not empty
if( !empty($email) && !empty($password) ){
if(canLogin($email,$password === true)){ // if email & password match
$_SESSION["email"] = $email;
header('Location: index.php'); // direct to index.php
} else{
$error = "Cannot log you in";
}
} elseif( empty($email) && empty($password) ){
$error = "Email & password are required";
} elseif(empty($email)){
$error = "Email is required";
} elseif(empty($password)){
$error = "Password is required";
}
}
更改此行
if(canLogin($email,$password === true)){
到
if(canLogin($email,$password) === true){
您在错误的位置将函数调用的结束)
放在错误的位置,使第二个参数成为布尔值