为什么登录后不会重定向头重定向?

时间:2016-02-19 09:46:57

标签: php redirect

我有登录页面的登录页面,当管理员登录时,它应该重定向到管理页面。这一切都有效,但是四天前它再次重定向到登录页面。当我在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>

在我的localhost服务器上,这是有效的,但在Web服务器(BLUEHOST)上,这在四天前停止工作。

有谁知道为什么会这样?

2 个答案:

答案 0 :(得分:2)

你不能在html之后使用标题。

两种解决方案:

任何HTML代码之前的标题(我最喜欢的)

缓冲

@UniqueEntity

完整代码:

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

答案 1 :(得分:1)

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