PHP网址重定向

时间:2014-02-22 16:48:21

标签: php html url http-headers

我为用户登录编写了一个PHP脚本,我使用2个用户验证它,即“test”和“admin”。当用户名和密码匹配时,我将页面重定向到UploadFile.php和DownloadFile.php。虽然我的脚本通过显示块中存在的echo stmt来验证测试和管理员用户,但重定向不起作用。我做了什么错误或者我应该采用其他任何方法?

<!DOCTYPE html>

<html lang="en" class="no-js"> <!--<![endif]-->
    <head>
        <meta charset="UTF-8" />
        <title>Login Page</title>
    </head>
    <body>

        <h1>PHP &amp; MySQL File Upload & Download </h1>
        <br><br><br>
        <form autocomplete="on" method="post"> 
            <h1>Log in</h1> 
            <p> 
                <label for="userName" class="uname" data-icon="u" > Your email or username </label>
                <input id="userName" name="userName" required="required" type="text" placeholder="myusername or mymail@mail.com"/>
            </p>
            <p> 
                <label for="userPass" class="youpasswd" data-icon="p"> Your password </label>
                <input id="userPass" name="userPass" required="required" type="password" placeholder="eg. X8df!90EO" /> 
            </p>
            <p class="keeplogin"> 
                <input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" /> 
                <label for="loginkeeping">Keep me logged in</label>
            </p>
            <p class="login button"> 
                <input type="submit" value="Login" id="sign" name="sign"/> 
            </p>
            <p class="change_link">
                Not a member yet ?
                <a href="#toregister" class="to_register">Join us</a>
            </p>
        </form>
    </body>
</html>

    <?php
    if (isset($_POST['sign'])) {

        $uname = test_input($_POST["userName"]);
        $upass = test_input($_POST["userPass"]);

        if ((strcmp($uname, "test") == 0) && (strcmp($upass, "test") == 0)) {
            header("Location: UploadFile.php");
            echo "test user";
        } else if ((strcmp($uname, "admin") == 0) && (strcmp($upass, "admin") == 0)) {
            header('Location: DownloadFile.php');
            echo "admin user";
        } else {
            echo "<script>
                alert('Login Failed');
            </script>";
        }
    }

    function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

    ?>

3 个答案:

答案 0 :(得分:1)

将PHP代码放在HTML代码前面。

在输出任何内容之前,必须先拨打header()来电!你之前输出了很多html。

有关详细信息,请参阅以下任何结果:https://www.google.de/search?q=headers+already+sent

答案 1 :(得分:0)

在启动响应正文后,您无法输出标题。尝试在HTML开始之前将PHP块移动到。

另一方面,PHP没有告诉您这一事实表明您的错误报告不够详细 - 您应该看到类似于this question中的警告。尝试将error_reporting(-1);放在代码顶部,或更改php.ini中的设置。

答案 2 :(得分:0)

一般来说,我同意使用header()作为正确的重定向机制(除了你注入的javascript之外,你可以将整个PHP代码块移到HTML之上以获得所需的效果)

但是,如果您想在执行重定向之前在短时间内(比如说2秒)显示一些内容,请考虑使用“元刷新”方法,如下所述:

http://www.w3.org/TR/WCAG20-TECHS/H76

例如,在您当前的代码中,尝试更改:

header("Location: UploadFile.php");

为:

echo "<meta http-equiv=\"refresh\" content=\"2;URL='UploadFile.php'\" />";

虽然W3C页面说你应该将这个元素放在“head”元素中,但实际经验表明绝大多数浏览器都会尊重你显示内容的意图,然后在指定的时间间隔后重定向。

如果您决定使用此“meta”元素并将PHP代码移到文件顶部,我建议相应地插入值而不是立即回显它们:

<?php
$meta_element = '';
$login_message = '';

if (isset($_POST['sign'])) {

    $uname = test_input($_POST["userName"]);
    $upass = test_input($_POST["userPass"]);

    if ((strcmp($uname, "test") == 0) && (strcmp($upass, "test") == 0)) {
    $meta_element = "<meta http-equiv=\"refresh\" content=\"2;URL='UploadFile.php'\" />";
        $login_message = "test user";
    } else if ((strcmp($uname, "admin") == 0) && (strcmp($upass, "admin") == 0)) {
    $meta_elementt = "<meta http-equiv=\"refresh\" content=\"2;URL='DownloadFile.php'\" />";
        $login_message = "admin user";
    } else {
        $login_message = "<script>
            alert('Login Failed');
        </script>";
    }
}

function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

?>

<!DOCTYPE html>

<html lang="en" class="no-js"> <!--<![endif]-->
<head>
    <meta charset="UTF-8" />
    <title>Login Page</title>
    <?php echo $meta_element; ?>
</head>
<body>

    <h1>PHP &amp; MySQL File Upload & Download </h1>
    <br><br><br>
    <form autocomplete="on" method="post"> 
        <h1>Log in</h1>
    <?php echo $login_message; ?>
        <p> 
            <label for="userName" class="uname" data-icon="u" > Your email or username </label>
            <input id="userName" name="userName" required="required" type="text" placeholder="myusername or mymail@mail.com"/>
        </p>
        <p> 
            <label for="userPass" class="youpasswd" data-icon="p"> Your password </label>
            <input id="userPass" name="userPass" required="required" type="password" placeholder="eg. X8df!90EO" /> 
        </p>
        <p class="keeplogin"> 
            <input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" /> 
            <label for="loginkeeping">Keep me logged in</label>
        </p>
        <p class="login button"> 
            <input type="submit" value="Login" id="sign" name="sign"/> 
        </p>
        <p class="change_link">
            Not a member yet ?
            <a href="#toregister" class="to_register">Join us</a>
        </p>
    </form>
</body>
</html>