1

this is my whole code, and i'm having an error in line header('location:../registrar/dasboard.php');

saying, Warning: Cannot modify header information, header already sent

below is my whole code

<?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>

I tried other solutions but i can'f find one, pls help, thanks

Drenyl
  • 906
  • 1
  • 18
  • 41
  • This is probably the most asked question on StackOverflow, please search first. – Matt Feb 07 '16 at 07:56
  • as @Source Matters said, you can't send header of set cookies after actual html-code was out. Do your checks before output. And, probably, here is one little bug, \n between ?> and – Wizard Feb 07 '16 at 08:44

2 Answers2

1

You can't output any HTML to the browser before you attempt a header redirect. The error is clear. If you want to redirect and output HTML before it, you'll have to use a client-side method such as a JavaScript window.location().

Source Matters
  • 1,110
  • 2
  • 15
  • 35
0

I have not tested. But try it like this. Just bring your form submission php code on the top of the form. And also the below code. You don't need to specify the php before include function.

      php include('navbar.php'); 

Just put it like this.

      include('navbar.php'); 

Try this possibly it Will work.

Kvvaradha
  • 732
  • 1
  • 13
  • 28