在前端我使用ReactJs,我一直在尝试通过axios.get从后端(PHP)获取登录用户的数据,为用户设置一个条件,如果满足,他们将返回登录页面后端没有返回任何内容,出现返回登录的情况是因为前端负责处理数据的文件是空的,所以没有返回任何内容。
我已经设置了会话,将 $_SESSION[] 替换为 $_POST[],并将这些代码片段放置在页面顶部:
error_reporting(E_ALL);
ini_set('display_errors','1');
但没有任何效果,并且错误没有显示。我也在 Postman 中进行了测试,它返回空,但是当我在 x-www-form-urlencoded 中测试时,所有内容都已设置并以任何可能的方式写入键和值,它返回 400 错误,所有字段都必须填写.
在下面,我将发布登录和注册的代码片段(其中处理用户的输入以发送到数据库)。
登录
$db = _db();
try {
$q=$db->prepare("SELECT * FROM users WHERE email = :email");
$q->bindValue(":email", $_POST["email"]);
$q->execute();
$row = $q->fetch();
if(!empty($row)){
if (password_verify($_POST["password"], $row["password"])) {
$user = array(
"user_id" => $row["user_id"],
"first_name" => $row["first_name"],
"last_name" => $row["last_name"],
"email" => $row["email"],
"company" => $row["company"],
"phone_number" => $row["phone_number"],
"verified" => $row["verified"],
"token" => $row["token"],
);
header("Content-type: application/x-www-form-urlencoded");
http_response_code(200);
echo json_encode($user);
exit();
} else {
header("Content-type: application/x-www-form-urlencoded");
http_response_code(400);
echo json_encode("Wrong email or password");
exit();
}
} else {
header("Content-type: application/x-www-form-urlencoded");
http_response_code(400);
echo json_encode("User does not exist");
exit();
}
注册 - 也在这里设置 $_SESSION,因为在确认帐户后,我计划将用户直接重定向到仪表板
$q=$db->prepare("INSERT INTO users(user_id, first_name, last_name, company, email, phone_number, password, forgot_password, token, verified) VALUES(:user_id, :first_name, :last_name, :company, :email, :phone_number, :password, :forgot_password, :token, :verified)");
$q->bindValue(":user_id", null);
$q->bindValue(":first_name", $firstName);
$q->bindValue(":last_name", $lastName);
$q->bindValue(":company", $company);
$q->bindValue(":email", $email);
$q->bindValue(":phone_number", $phoneNumber);
$q->bindValue(":password", $passwordHash);
$q->bindValue(":forgot_password", $forgotPass);
$q->bindValue(":token", $token);
$q->bindValue(":verified", false);
$q->execute();
$user_id = $db->lastInsertId();
$to_email = $email;
$subject = "Email subject";
$message = "Click on the following link to verify your account:
<a href='http://localhost/api/confirm_account.php?token=$token'>Confirm your account</a>";
require_once(__DIR__."/emailVerification/send_email.php");
$user = array(
"user_id" => $_SESSION["user_id"] = $row["user_id"],
"first_name" => $_SESSION["first_name"] = $row["first_name"],
"last_name" => $_SESSION["last_name"] = $row["last_name"],
"email" => $_SESSION["email"] = $row["email"],
"company" => $_SESSION["company"] = $row["company"],
"phone_number" => $_SESSION["phone_number"] = $row["phone_number"],
"verified" => $_SESSION["verified"] = $row["verified"],
"token" => $_SESSION["token"] = $row["token"],
);
header("Content-type: application/json");
http_response_code(200);
echo json_encode($user);
exit();
}
}
这就是我在前端处理 POST/GET 请求的方式
const handleLogInSubmit = (event) => {
event.preventDefault();
axios
.post("http://localhost/api/login.php", {
email: userInput.email,
password: userInput.password,
})
.then((response) => {
if (response.data) {
navigate("/dashboard");
} else {
setErrorMessage("Login failed, try again");
}
})
useEffect(() => {
axios.get("http://localhost/api/login.php")
.then((response) => {
if(response.data){
setUser(response.data);
console.log(response.data)
} else {
console.log(response)
navigate("/login");
}
});
}, [navigate]);
login.php 文件的输出
如果您需要查看其余的前端和后端代码,请告诉我。
编辑:
现在,我可以看到代码出了什么问题,但即使在收到消息登录成功之后,我仍然不断被重定向到登录页面,只是看到 axios.get不检索任何内容,仅 axios.post 起作用:
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
问题是您从前端发送
JSON数据,但尝试使用$_POST来提取它 根据文档适用于x-www-form-urlencoded或multipart/form-data表单数据。您可以发送后端期望的数据,而不是发送 JSON 数据,这是一串查询参数:
.post("http://localhost/api/login.php", new URLSearchParams({ email: userInput.email, password: userInput.password, }))