PHP无法正确重定向

时间:2017-03-27 10:46:57

标签: php html

我有以下PHP代码名为recover.php:

<?php

include "php/init.php";
inaccessible_if_loggedIn();

if (isset($_GET['success']) === true && empty($_GET['success']) === true) {

?>
    <p>succes!</p>

<?php
} else {

    $allowed_modes = array('username', 'password');
    if (isset($_GET['mode']) === true && in_array($_GET['mode'], $allowed_modes) === true) {

        if (isset($_POST['email']) && empty($_POST['email']) === false) {

            if (user_in_DB($_POST['email'])) {
                // TO DO: schrijf recover functie
                //recover($_GET['mode'], $_POST['email']);
                header("Location: recover.php?success");
                exit();
            } else {
                $errors[] = "email: " . $_POST['email'] . " does not exist";
            }

        }

        include "includes/recover_form.php";
    } else {
        header("Location: includes/errorPages/page_not_exist.php");
        exit();
    }

}


?>

html include包含一个带有动作的表单recover.php

html代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="css/signInStylesheet.css">
    <title>bestBay</title>
</head>
<body>

<div class="wrapper">

    <div class="header_container">
        <a id="logoLink" href="index.php"><img src="images/logo.png" class="logo"></a>
    </div>

    <div class="register_form">

        <div class="formBody">

            <form action="recover.php" method="post">

                <br/>

                <span class="formText">E-Mail<span style="color: red">*</span></span> <input name="email" class="fillInput" type="email" maxlength="90" required>

                <br/>
                <br/>

                <?php echo print_errors($errors); ?>

                <input class="signInButton" type="submit" value="Recover">
            </form>

        </div>
    </div>


</div>


</body>
</html>

页面布局:

html layout

问题是在用户输入有效的电子邮件后,我的PHP代码仍然重定向到“includes / errorPages / page_not_exist.php”,好像链接后面的?成功不存在。

我无法在代码中看到我做错了什么。 如果我离开

else {
        header("Location: includes/errorPages/page_not_exist.php");
        exit();
    }

清空我的代码似乎工作。 我到底错过了什么?

2 个答案:

答案 0 :(得分:0)

为什么你的代码中有这么多的php标签?请删除这些标签并回显成功,也尝试使用elseif代码的第二部分......

&GT?;     

更迭!

包括“php / init.php”; inaccessible_if_loggedIn();

if(isset($ _ GET ['success'])=== true&amp;&amp; empty($ _ GET ['success'])=== true){

echo succes!;

} elseif {

//请使用ini_set(“display_startup_errors”,1)在起始行上记录错误; ini_set(“display_errors”,1);

答案 1 :(得分:0)

解决方案相当简单。我的代码甚至没有甚至重定向到成功页面。这是因为我的表单操作从未传递过GET变量,导致$ _GET ['mode returns returns']。

简单的解决方案是将动作留空。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="css/signInStylesheet.css">
    <title>bestBay</title>
</head>
<body>

<div class="wrapper">

    <div class="header_container">
        <a id="logoLink" href="index.php"><img src="images/logo.png" class="logo"></a>
    </div>

    <div class="register_form">

        <div class="formBody">

            <form action="" method="post">

                <br/>

                <span class="formText">E-Mail<span style="color: red">*</span></span> <input name="email" class="fillInput" type="email" maxlength="90" required>

                <br/>
                <br/>

                <?php echo print_errors($errors); ?>

                <input class="signInButton" type="submit" value="Recover">
            </form>

        </div>
    </div>


</div>


</body>
</html>