header already sent

Discussion in 'PHP' started by salayhin, Oct 13, 2009.

  1. #1
    I have a problem in my following code.
    THis code show a massege.Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\bangla_music_store\music-admin\login.php:12) in C:\xampp\htdocs\bangla_music_store\music-admin\login.php on line 40

    Please solve this.

    <?php
    session_start();
    ?>
    <html>

    <head>
    <title>.::Log In::.</title>
    </head>

    <body>

    <?php
    include_once('menubar.php');
    add_header();
    //admin_menu();
    include_once ("config/common_db.inc");
    $link_id=db_connect();
    $uname=$_POST['username'];
    $password=$_POST['pass'];

    $query="SELECT * FROM music_admin WHERE user_name='$uname' AND admin_password ='$password'";
    $result=mysql_query($query);
    $row=mysql_fetch_array($result);
    //Find Access Level
    $user_name=$row[2];
    $pass=$row[3];
    $access_level=$row[5];
    $usr_id=$row[0];

    if(mysql_num_rows($result)>0)
    {
    //Redirect User As Access Level
    switch($access_level)
    {
    case "1":
    header("Location:.php");
    break;

    case "2":
    header('Location: add_admin.php');
    break;

    case "3":
    header("Location: .php");
    break;

    default:
    echo "No number between 1 and 3";
    }


    //header('location:add_admin.php');
    }
    else
    {
    echo "<br>";
    echo "<br>";
    echo "<br>";
    echo "<div align=\"center\">";
    echo "<h3>Your Username or Password is Wrong! You Will Redirect To The Log In Page </h3>";
    header('Location: index.php');
    echo "<h3>If Your Brower Doesn't Suport It, Please Log In<a href=\"index.php\">Here</a></h3>";
    echo "</div>";
    }
    ?>

    </body>

    </html>
     
    salayhin, Oct 13, 2009 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    If you wish to redirect user via header you should do it before anything is sent to browser. In your script html tags are sent before headers, that's why you get this message.
     
    AsHinE, Oct 13, 2009 IP
  3. Oli3L

    Oli3L Active Member

    Messages:
    207
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    70
    #3
    add this before everything:
    
    <?php
    ob_start();
    ?>
    
    PHP:
    and this after everything:
    
    <?php
    ob_flush();
    ?>
    
    PHP:
     
    Oli3L, Oct 13, 2009 IP
  4. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #4
    I am pretty sure you have to have all headers before any HTML.
     
    Pudge1, Oct 13, 2009 IP
  5. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    use ob_start() as very first statement in your script flow of execution
     
    lmao, Oct 14, 2009 IP