php header 重定向.. 不工作

php header to redirect.. not working

我已经检查并重新检查了,但我不知道我做错了什么。没有出现任何错误,它只是在我提交登录页面后没有指示我去哪里。有什么建议吗?

header:

    <html>
<head>
<title><?php echo $title; ?></title>
<style type ="text/css">


#top_links  a:link, a:visited{
    width: 100%;
    display: block;
    font-weight: bold;
    color: #FFFFFF;
    background-color: black;
    text-align: center;
    padding: 4px;
    text-decoration: none;
    text-transform: uppercase;
    border: none;
    font-family: "Trebuchet MS", Helvetica, sans-serif;
}

#top_links ul{
    display: table-row;
}

#top_links li{
    display: table-cell;
    margin: 0;
}

#top_links a:hover {
    color: pink;

}

</style>
</head>

<body<div id="top_links">
<ul>
    <li><a href="REGISTRATION_FORM&HANDLE.php">Register</a></li>
    <li><a href="LOGIN.php">Login</a></li>
</ul>
</div>

LOGIN.php 文件:

<?php

require_once('../../../secure_files/mysql_connect.php');
$title = 'Login';
include_once('header.php');
if(isset($_POST['validate'])) {
    $errors = array();
    function validate_func($value, $msg, $val_type) {
        global $link;
        switch ($val_type) {
            case 'string':
                if(empty($value)){
                    $errors[] = "You forgot to enter your email ".$msg;
                }else{
                    $value = mysqli_real_escape_string($link, trim($value));
                }
                break;

            case 'password':
                if(empty($value)) {
                    $errors[] = "You forgot to enter your email ".$msg;
                }else{
                    $value = trim($value);
                }
                break;
            case 'number':
                if(!isset($value)||!is_numeric($value)) {
                    $error[] = "You forgot to enter ".$msg." or the value you entered is not a number.";
                }else{
                    $value = trim($value);
                }
                break;
        }
        return $value;
    }

    $email = validate_func($_POST['email'], "email", "string");
    $password = validate_func($_POST['password'], "password", "password");  
    if(!count($errors) != 0){
            foreach($errors as $value) {
                echo $value." <br />";
            }
    }else {
        $select_guest = "SELECT GUEST_INFO_ID FROM GUEST_INFO WHERE EMAIL = '$email' AND PASSWORD = sha1('$password') LIMIT 1";
        $exec_select_guest = @mysqli_query($link, $select_guest);
            if(mysqli_num_rows($exec_select_guest) != 1) {
                echo "You are not an authentic user, you are being directed to the registration page...";
                mysqli_close($link);
                header("Refresh:3; url='REGISTRATION_FORM&HANDLE.php'");
            }else{
                $one_record = @mysqli_fetch_row($exec_select_guest);
                setcookie('GUEST_INFO_ID', $one_record[0], 0, '/', '', 0, 0);
                echo "You are an authentic user";
                header("Refresh:3; url='GUEST_MAIN_MENU.php'");             
            }
    }

} else{
?>
    <div id="LOGIN_MAIN">
        <form action="<?php echo $_SERVER['PHP_SELF'];?>" method = "post" >

            <div>
            Email:<input type="text" name="email"  id="email"  />
            </div>

            <div>
            Password:<input type='password' name='password' id='password' />
             </div>

             <div>
            <input type='submit' name='submit' id='submit' value='Submit' />
            <input type='reset' name='reset' id='reset' value='Reset' />    
            <input type="hidden" name="validate" ID="validate" value="Reset" />
            </div>

        </form>

    </div>

<?php
}

include('footer.php');
?>

和我的页脚:

</body>
</html>

原因是,您在设置 headers 之前回显了一些内容。

任何 header 必须在任何其他输出之前才有效。
php-manual for header()

所以删除

echo $value." <br />";
echo "You are not an authentic user, you a...";

或 header-redirection 之前的任何输出,它会起作用!

如果您想在用户看到响应后进行重定向,则必须使用 javascript 重定向! 那将是这样的:

<script>
// redirects after 3 seconds
setTimeout(function(){ 
      window.location.href = "GUEST_MAIN_MENU.php";; 
   }, 3000);

</script>

旁注:
无论如何,我建议在不加载新的(或相同的)php-script 的情况下测试用户凭据。看看javascriptajax!使用此技术,用户将停留在同一页面上并获得更即时的响应,您的应用也可以通过消息和重定向做出响应。