I am getting header already sent error in php

Discussion in 'PHP' started by seotraining, Nov 6, 2010.

  1. #1
    I am using

    header("Lcoation:index.php") to redirect to index page but i am getting error there that header already sent. How can I fix it?
     
    seotraining, Nov 6, 2010 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    header("Location:index.php");

    common cause for this error is remove echo or print statments before it

    dont write anything on client side before this statement

    Regards

    Alex
     
    kmap, Nov 6, 2010 IP
  3. lazcorp

    lazcorp Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    kmap, may be you mean server side, not client. Because client side is the browser.

    seotraining, if you cant find where you start the output to the browser you can user ob_start() function. Put it at the very first line of the script - right after <?
     
    lazcorp, Nov 6, 2010 IP
  4. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Also, if you have any "white space" at the bottom of your file, it will cause this error when trying to modify headers.

    Make sure there are not lines or even a space after your last characters of of code (meaning: ?> or </body>)
     
    fr33lanc3, Nov 6, 2010 IP
  5. lazcorp

    lazcorp Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That is true only if the file in which have space is included before Header() function. Otherwise if the space is AFTER the Header() it does not matter and it isn't problem!
     
    lazcorp, Nov 6, 2010 IP
  6. fr33lanc3

    fr33lanc3 Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Right, I failed to mention it could be in any included files too. That error can be a headache to debug.
     
    fr33lanc3, Nov 6, 2010 IP
  7. redditor

    redditor Guest

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    There has to be something being printed or echoed before the header function if you got that error. You need to remove it.
     
    redditor, Nov 6, 2010 IP
  8. pjshaun

    pjshaun Peon

    Messages:
    11
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Check for any 'echo' or 'print' statement before your header () function.
     
    pjshaun, Nov 6, 2010 IP