Php header函数不工作
我创建了一个注册表单,一旦用户成功注册,它应该重定向到home.php。但是,它被重定向到index.php。其他一切都正常(请参见下面的代码)。我可以看到一个用户被添加到我的数据库中,并且配置文件图片的状态为1Php header函数不工作,php,if-statement,mysqli,phpmyadmin,Php,If Statement,Mysqli,Phpmyadmin,我创建了一个注册表单,一旦用户成功注册,它应该重定向到home.php。但是,它被重定向到index.php。其他一切都正常(请参见下面的代码)。我可以看到一个用户被添加到我的数据库中,并且配置文件图片的状态为1 <?php if (isset($_POST['submitSignup'])) { include_once 'dbh.inc.php'; $first = $_POST['first']; $last = $_POST['last']; $email = $_POST[
<?php
if (isset($_POST['submitSignup'])) {
include_once 'dbh.inc.php';
$first = $_POST['first'];
$last = $_POST['last'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['passwordS'];
if (empty($first) || empty($last) || empty($email) || empty($username) || empty($password)) {
header("Location: ../signup.php?signup=empty"); exit();}
else{
if (!preg_match("/^[A-Za-z\s'-]{2,50}$/", $first) || !preg_match("/^[A-Za-z\s'-]{2,50}$/", $last)) {
header("Location: ../signup.php?signup=flnameinvalid&email=$email&username=$username"); exit();}
else{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?signup=emailinvalid&first=$first&last=$last&username=$username"); exit();}
else{
$mysql = "SELECT * FROM users WHERE user_username='$username';";
$result = mysqli_query($conn, $mysql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
header("Location: ../signup.php?signup=unametaken&first=$first&last=$last&email=$email"); exit();}
else{
if (strlen($password) < 8) {
header("Location: ../signup.php?signup=strlen&first=$first&last=$last&email=$email&username=$username"); exit();}
else{
if (!preg_match("#[0-9]+#", $password)) {
header("Location: ../signup.php?signup=num&first=$first&last=$last&email=$email&username=$username"); exit();}
else{
if (!preg_match("#[A-Z]+#", $password)) {
header("Location: ../signup.php?signup=cap&first=$first&last=$last&email=$email&username=$username"); exit();}
else{
$hashedPwd = password_hash($password, PASSWORD_DEFAULT);
$sql = "INSERT INTO users (user_first, user_last, user_email, user_username, user_password)
VALUES (?, ?, ?, ?, ?);";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare ($stmt, $sql)) {
echo "SQL error!";}
else{
mysqli_stmt_bind_param ($stmt, "sssss", $first, $last, $email, $username, $hashedPwd);
mysqli_stmt_execute ($stmt);
$sql3 = "SELECT * FROM users WHERE user_username='$username' AND user_first='$first';";
$result = mysqli_query($conn, $sql3);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$userid = $row['id'];
$sql4 = "INSERT INTO profileimg (userid, status) VALUES ($userid, 1);";
mysqli_query($conn, $sql4);
}
}
else{ echo "There are no users!";}
}
header("Location: ../home.php?signup=success"); exit();
}
}
}
}
}
}
}
}
else{header("Location: ../signup.php?signup=error");}
试试看
echo”“。
“window.location='signup.php?signup=error';”。
"";
你可以试试这个,它对我有用
将此代码替换到您的标题位置
<script type=\"text/javascript\">
window.location.href='home.php';
</script>
window.location.href='home.php';
if-remove last if and else(不是错误,而是…)是否仍然有效?是否值得检查以避免执行额外的查询。不必通过重定向发送如此多的值,您可以将它们存储在会话中并重定向到页面,而无需任何参数。然后在将这些会话放回表单/显示消息后取消设置。@BhumiShah,不,它不再工作。我也在其他文件中添加了一些代码。我更新了我的帖子,你能检查一下吗?你在$_SESSION['key']中获得了价值吗?你能补充一些解释,说明你的答案是什么,以及它是如何回答OP的问题的。为什么你会建议使用JavaScript重定向而不是从服务器发送正确的重定向?这很有效,但不是一种好的方法。修复程序,使header()
完成工作通常会更好。
echo "<script type=\"text/javascript\">".
"window.location='signup.php?signup=error';".
"</script>";
<script type=\"text/javascript\">
window.location.href='home.php';
</script>