为什么登录后标头重定向不起作用


Why won't header redirect work after login?

我有一个带有登录表单的登录页面,当管理员登录时,它应该重定向到管理页面。这一切都有效,但四天前它再次重定向到登录页面。当我在登录 url 后手动键入 admin 时,可以访问管理页面。

我的登录页面:

<html>
    <head>
        
        <?php include 'connect.php'; ?>
        <?php include 'functions.php'; ?>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, minimumscale=1.0, maximum-scale=1.0" />
        <title>Login - Admin</title>
        <link rel='stylesheet' href='style.css' type='text/css' />
        <?php include 'header.php'; ?>
    </head>
    <body >
        <div id="container_vanjski">
        <div id="container">
       
            <form method="post">
                <br/>
                <?php
                if(isset($_POST['submit'])){
                    $username = mysqli_real_escape_string($con, $_POST['username']);
                    $password = md5(mysqli_real_escape_string($con, $_POST['password']));
                    if(empty($username) or empty($password)){
                        echo '&nbsp<p>Polja su prazna !</p>';
                    }
                    else {
                        $check_login = mysqli_query($con, "SELECT id, user_level FROM korisnici WHERE username='".$username."' AND password='".$password."'");
                        
                                                             
                        if(mysqli_num_rows($check_login) == 1){
                            $run = mysqli_fetch_array($check_login);
                            $user_id = $run['id'];
                            $user_level = $run['user_level'];
             		
                            $_SESSION['user_id'] = $user_id;
                                header("Location: admin");                     
                        }else{
                            echo '&nbsp<p>Pogrešno Korisničko ime ili Lozinka!</p>';
                        }
                    }
                }
            ?>
                <br/>
                <div id="log"> 
            
            <label for="username">Korisničko ime:</label><input type="text" name="username" /><br />
            <label for="password">Lozinka:</label><input type="password" name="password" /><br />
            <br />
            <input type="submit" name="submit" value="Prijava" id="button" />
            
                </div>
        </form>
 
        </div>
        
        <?php include 'footer.php'; ?>
        </div>
    </body>
</html>

在我的本地主机服务器上,这正在工作,但在 Web 服务器 (BLUEHOST) 上,这在四天前停止工作。

有谁知道为什么会这样?

你不能在 html 后面使用标头。

两种解决方案:

任何 html 代码之前的标题(我的最爱)

缓冲

 <?php
    ob_start( );
?>
<html>
    <body>
        some output
        <?php
            ob_end_clean( );
            header( 'Location: http://www.google.com' );
            exit;
        ?>
    </body>
</html>
<?php
      ob_end_flush( );
?>

完整代码 :

<?php
    include 'connect.php';
    include 'functions.php';
    if(isset($_POST['submit'])){
        $username = mysqli_real_escape_string($con, $_POST['username']);
        $password = md5(mysqli_real_escape_string($con, $_POST['password']));
        if(empty($username) or empty($password)){
            $message = '&nbsp<p>Polja su prazna !</p>';
        }
        else {
            $check_login = mysqli_query($con, "SELECT id, user_level FROM korisnici WHERE username='".$username."' AND password='".$password."'");

            if(mysqli_num_rows($check_login) == 1){
                $run = mysqli_fetch_array($check_login);
                $user_id = $run['id'];
                $user_level = $run['user_level'];
                $_SESSION['user_id'] = $user_id;
                    header("Location: admin");                     
            }else{
                $message = '&nbsp<p>Pogrešno Korisničko ime ili Lozinka!</p>';
            }
        }
    }
?>
<html>
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, minimumscale=1.0, maximum-scale=1.0" />
        <title>Login - Admin</title>
        <link rel='stylesheet' href='style.css' type='text/css' />
        <?php include 'header.php'; ?>
    </head>
    <body >
        <div id="container_vanjski">
        <div id="container">
            <form method="post">
                <br/>
                <?php echo (isset($message) ? $message : ''); ?>
                <br/>
                <div id="log"> 
            <label for="username">Korisničko ime:</label><input type="text" name="username" /><br />
            <label for="password">Lozinka:</label><input type="password" name="password" /><br />
            <br />
            <input type="submit" name="submit" value="Prijava" id="button" />
                </div>
        </form>
        </div>
        <?php include 'footer.php'; ?>
        </div>
    </body>
</html>

必须在服务器上生成任何其他内容之前发送标头。将登录处理代码移动到文件开头的开始 HTML 标记之前。它可能已经在您的本地主机上工作,因为某些配置对于标头在代码中发送可能有点宽容,但它不合规,您可能会发现这就是问题所在。