我正在创建一个学校网站,允许学生和老师用于家庭作业。我尝试在不同的浏览器上运行它,它仍然说它被重定向了太多次。
if(empty($first_name) || empty($last_name) || empty($user_password) ||
empty($user_positon) || empty($user_department) || empty($user_status))
{
header("Location: signup.php");
exit();
}
else if (!filter_var($email, FILTER_VALIDATE_EMAIL) &&
!preg_match("/^[a-zA-Z0-9]*$/", $first_name)) {
header("Location: ../signup.php?error=invalidmailandusername");
exit();
}
else if (!filter_var($email, FILTER_VALIDATE_EMAIL)){
header("Location: ../signup.php?error=invalidmail");
exit();
}
else if (!preg_match("/^[a-zA-Z0-9]*$/", $first_name)){
header("Location: ../signup.php?error=invalidusername");
exit();
} else {
$sql = "SELECT * FROM users_security_keys WHERE users_fname =
'$first_name'
AND users_lname = '$last_name';";
$stmt = mysqli_stmt_init($connection_String);
if(!mysqli_stmt_prepare($stmt, $sql)) {
echo "<script>alert('There was an error')</script>";
exit();
} else {
mysqli_stmt_bind_param($stmt, "s", $first_name);
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);
$resultCheck = mysqli_stmt_num_rows($stmt);
if (resultsCheck > 0) {
header("Location: ../signup.php?error=usertaken&mail=".$email);
exit();
} else {
$sql = "INSERT INTO users_table (`Users_ID`, `user_fname`, `user_lname`,
`department`, `position`, `Password`, `Profile_Picture`, `bio`) VALUES
(NULL, '$first_name', '$last_name', '$user_department', '$user_position',
'$user_password', '', '')";
$stmt = mysqli_stmt_init($connection_String);
if(!mysqli_stmt_prepare($stmt, $sql)) {
echo "<script>alert('There was an error.')</script>";
exit();
} else {
$hashpass = password_hash($user_password, PASSWORD_DEFAULT);
mysqli_stmt_bind_param($stmt, "sss", $first_name, $email, $hashpass);
mysqli_stmt_execute($stmt);
echo "<script>alert('You have registered Successfully')</script>";
return "index.php";
exit();
}
}
}
}
mysqli_stmt_close($stmt);
mysqli_close($connection_String);
} else {
header("Location: signup.php");
exit();
}
请帮助我,因为我还是PHP的初学者,请随时告诉我我的错误,以便我可以改进。
发布于 2018-12-17 15:35:53
如果此代码是您的signup.php
文件,那么这是预期的行为。
当您请求/signup.php
使用空参数(no $first_name
,。,。,。)触发此文件时,由于此行,该参数将再次重定向到自身
if(empty($first_name) || empty($last_name) || empty($user_password) ||
empty($user_positon) || empty($user_department) || empty($user_status))
{
header("Location: signup.php");
exit();
// to fix this you echo the HTML here instead of redirect
}
如果你想要1个php脚本来执行这两个操作(显示注册表单页面并注册新用户),那么我建议你用更简洁的方式将这两个操作分开,就像请求方法一样
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
//signing up new users action (do the rest of your code here)
}else{
// showing the signup form action (show the signup HTML with empty form)
// echo $HTML;
}
https://stackoverflow.com/questions/-100006307
复制相似问题