簡體   English   中英

頁面被重定向到錯誤的頁面

[英]page getting redirected to wrong page

我有一個簡單的登錄表單,該表單可以正常工作,但允許用戶登錄,但是在某種情況下,我希望將其重定向到其他頁面,而不是常規索引頁面。

我面臨的問題的一段代碼

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'];獲得價值$spid=$_GET['spid']; 因此,與其遵循上述方法,不如將$spid用作表單中的隱藏i / p文本,然后將其作為代碼傳遞給您

$finalspid=$_POST["spid"]

然后根據$finalspid放置您的if else條件

嘗試這個:

if(!isset($spid) && $spid==''){
   header('Location:index.php');
} else {
   header("Location:new.php?id=".$spid);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM