PHP:标头未重定向到给定位置
我只想问一下,为什么我在PHP中的头没有重定向,尽管所有语句都是正确的,并且运行正常 index.php:PHP:标头未重定向到给定位置,php,header,Php,Header,我只想问一下,为什么我在PHP中的头没有重定向,尽管所有语句都是正确的,并且运行正常 index.php: <form action="php/Api/verifyUser.php" method="post"> username: <input type="text" name="username"><br> password: <input type="text" name="password"><br> &
<form action="php/Api/verifyUser.php" method="post">
username: <input type="text" name="username"><br>
password: <input type="text" name="password"><br>
<input type="submit">
</form>
用户名:
密码:
php/Api/verifyUser.php:
<?php
include "../userDAO.php";
session_start();
//$content = file_get_contents("php://input");
//$json_data = json_decode($content, true);
$username = $_POST["username"];
$password = $_POST["password"];
$userDAO = new UserDAO();
$process = $userDAO->verifyUser($username, $password);
if($process["state"] == 1) {
echo "Successfully Log in!";
header("location : ../../homepage.php/");
} else {
echo "failed to Log in!";
header("location : ../../login.php");
}
?>
注意:
userDAO instancess工作正常,如果身份验证为true,则状态为“1”
verifyUser.php中的警告:
未声明HTML文档的字符编码。如果文档包含US-ASCII范围之外的字符,则在某些浏览器配置中,文档将呈现乱码文本。页面的字符编码必须在文档或传输协议中声明
如果条件为false,则上述代码将与失败消息相同地回显“成功登录!”消息。但是标题没有重定向到它的位置?这里有什么问题吗?谢谢。
<?php
include "../userDAO.php";
session_start();
//$content = file_get_contents("php://input");
//$json_data = json_decode($content, true);
$username = $_POST["username"];
$password = $_POST["password"];
$userDAO = new UserDAO();
$process = $userDAO->verifyUser($username, $password);
if($process["state"] == 1) {
header("location : ../../homepage.php/");
} else {
header("location : ../../login.php");
}
?>
头必须在任何形式的输出之前发送。删除标题前的“echo”,它就会工作。if($process==1){header(“location:../../../homepage.php/”)}或者{header(“location:../../login.php”);}
如果打开显示错误
或者检查错误日志,您应该会看到“headers ready sent”错误。阅读更多:使用ob_start();在你的页面上嗨,伙计们,我试图删除echo,但仍然不起作用。我刚刚编辑了我的帖子,我有一个警告,感谢您的快速响应:)如果您想显示消息,请在Session等中设置,例如$_Session['msg'],并将其显示在homepage.php和login.php中。我希望它对你有用。