php - 页面被重定向到错误的页面

标签 php mysql sql if-statement

我有一个简单的登录表单,允​​许用户登录,尽管该表单工作正常,但有一个条件是我希望将其重定向到其他页面,而不是常规索引页面。

我遇到问题的代码是

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/

相关文章:

php - 帮我查询

java - Hibernate 相当于 getTimestamp() 和 getInt()

如果条目是数字,PHP 仅删除行

php - 插入从另一个表中获取的 id

php - Yii第二个查询参数

php - 使用php从数据库中获取多行

mysql - 从表中获取第二高的值

php - XAMPP - 服务器和客户端无法通信

php - 比较相同的数组值

mysql - 导入文件时出现错误 "1366 Incorrect integer value: ' 1'"