我有一个简单的登录表单,允许用户登录,尽管该表单工作正常,但有一个条件是我希望将其重定向到其他页面,而不是常规索引页面。
我遇到问题的代码是
if($spid=="")
{
header('Location:index.php');
}
else
{
header('Location:new.php?id=$spid');
}
问题是即使 $spid 有一个值,它也会被重定向到 index.php 页面。谁能告诉我为什么会发生这种情况
提取上述代码的完整代码是
<?php
$spid=$_GET['spid'];
$emailErr = $pswrdErr = $loginErr = "";
$email = $password = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["email"])) {
$emailErr = "Email is required";
}
else {
$email = test_input($_POST["email"]);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$emailErr = "Invalid email format";
}
}
if (empty($_POST["password"])) {
$pswrdErr = "password is required";
}
else {
$password = test_input($_POST["password"]);
}
$sql = "SELECT * FROM usertable where email='".$email."' and password='".$password."' ";
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
$_SESSION['loggedin'] = true;
$_SESSION['email'] = $email;
if($spid=="")
{
header('Location:index.php');
}
else
{
header('Location:new.php?id=$spid');
}
}
}
else {
$loginErr = "Invalid Credentials";
}
}
function test_input($data) {
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST" role="form">
<input type="text" class="form-control" name="email">
<input type="password" class="form-control" name="password">
</form>
最佳答案
正如您所说,您在 $spid=$_GET['spid'];
中获得值(value),因此不要遵循上述方法,而是将您的 $spid
隐藏为 i/p 表单内的文本,然后将其作为
$finalspid=$_POST["spid"]
然后根据$finalspid
输入if else条件
关于php - 页面被重定向到错误的页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31960710/