PHP - 错误无法修改header信息

PHP - Error cannot modify header information

这是我的全部代码,我在行 header('location:../registrar/dasboard.php');

中遇到错误

说, 警告:无法修改header信息,header已发送

下面是我的全部代码

<?php
session_start();?>
<?php include('header.php');
php include('navbar.php');
php include('footer.php')?>
    <div class="container">
        <div class="margin-top">
            <div class="row">   
<center>
        <img src="../kcwa/kingsway.jpg" class="img-rounded">
</center>
                <div class="login">
                <div class="log_txt">
                <p><strong>Please Enter the Details Below..</strong></p>
                </div>
                        <form class="form-horizontal" method="POST">
                                <div class="control-group">
                                    <label class="control-label" for="inputEmail">Username :</label>
                                    <div class="controls">
                                    <input type="text" name="username" id="username" placeholder="Username" autofocus required>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label" for="inputPassword">Password :</label>
                                    <div class="controls">
                                    <input type="password" name="password" id="password" placeholder="Password" required>
                                </div>
                                </div>
                                <div class="control-group">
                                    <div class="controls">
                                    <button id="login" name="submit" type="submit" class="btn"><i class="icon-signin icon-large"></i>&nbsp;Submit</button>
                                </div>
                                </div>
                                </form>

                                <?php
                                if (isset($_POST['submit'])){
                                $username = $_POST['username'];
                                $password = $_POST['password'];
                                $query = "SELECT * FROM users WHERE username='$username' AND password='$password'";
                                $result = mysql_query($query)or die(mysql_error());
                                $num_row = mysql_num_rows($result);
                                    $row=mysql_fetch_array($result);
                                    if( $num_row > 0 ) {
                                        header('location:/registrar/dasboard.php');
                                $_SESSION['id']=$row['user_id'];
                                    }
                                    else{?>
                                <div class="alert alert-danger">Access Denied</div>     
                                <?php
                                }}
                                ?>
                </div>
            </div>      
            </div>
        </div>
    </div>

我尝试了其他解决方案,但我找不到一个,请帮忙,谢谢

在尝试 header 重定向之前,您无法向浏览器输出任何 HTML。错误很明显。如果你想在它之前重定向和输出 HTML,你必须使用 client-side 方法,例如 JavaScript window.location().

我没有测试过。但是像这样尝试。只需将您的表单提交 php 代码放在表单顶部即可。还有下面的代码。您不需要在包含函数之前指定 php。

      php include('navbar.php'); 

就这么说吧。

      include('navbar.php'); 

尝试这个可能会奏效。