help me on this error

Discussion in 'PHP' started by mirosoft1, Jan 15, 2008.

  1. #1
    i have two textbox and i check if the user insert this username and this password then go to another page else write wrong data and it is the code

    <?php
    $name="test";
    $password="test";
    if($_POST["admin_name"]==$name and $_POST["pass"]==$password)
    { header("location: /form/index.php");}
    else
    {echo"wrong data";}
    ?>

    but this code give me this error
    Warning: Cannot modify header information - headers already sent

    i donot know what i do
    please i want help


    thank you
     
    mirosoft1, Jan 15, 2008 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you are using header(), you have to make sure that there is no other output being generated before you call it.

    A common cause of this problem is blank lines in your file before the first <?

    It's also possible that you've saved your PHP file as unicode with BOM (byte-order mark).
     
    SmallPotatoes, Jan 15, 2008 IP
  3. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    i have a table on the page before the code and i save the php file ANSI not unicode
    what will i do????
     
    mirosoft1, Jan 15, 2008 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can't have any output before the header() call.

    Also, it doesn't make any sense to generate output if you are going to be sending a redirect header, since the browser will not display it.

    But if it's too much work to reorganise your code, you can just cheat, and use output buffering.

    <?
    
    ob_start();
    
    // output the table
    
    if (whatever_condition)
    {
       ob_end_clean();
       header("Location: elsewhere");
       exit;
    }
    
    ob_end_flush();
    
    // do something else
    
    ?>
    
    PHP:
     
    SmallPotatoes, Jan 16, 2008 IP
  5. shyju

    shyju Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    header() always() use before rendering any output.

    so u can use javascript for this

    <?php
    $name="test";
    $password="test";
    if($_POST["admin_name"]==$name and $_POST["pass"]==$password)
    { echo "<script language='javascript'>window.location='/form/index.php'</script>";}
    else
    {echo"wrong data";}
    ?>

    OK
     
    shyju, Jan 16, 2008 IP
  6. mirosoft1

    mirosoft1 Well-Known Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #6
    yes it work good thank you very much
     
    mirosoft1, Jan 16, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    Don't use Javascript, because the user can disable it and then your script wouldn't redirect anymore.
     
    nico_swd, Jan 16, 2008 IP