header("Location: url");

Discussion in 'PHP' started by lektrikpuke, Mar 23, 2008.

  1. #1
    Hi guys,

    I have a form page where I want to send the user back to the same page if a condition is not met. I've tried header("Location: url"); as suggested in the book I'm using, but that results in an error (cannot modify header - already sent).

    So, having done a bit of research, it would appear I'm using that the wrong way. What should I be doing?

    As always, you guys rock, and much appreciated. :D
     
    lektrikpuke, Mar 23, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Hello,

    If you turn output buffering on (put ob_start() at the VERY top of your code), you can avoid this error. The problem is being caused by you outputting content, then trying to redirect via the headers. Output buffering will hold everything in the buffer, and flush it all at once (so you can send headers after content), for the price of a page loading delay (i.e. it will all load at once).

    Jay
     
    jayshah, Mar 23, 2008 IP
  3. Jean-Luc

    Jean-Luc Peon

    Messages:
    601
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You may not send anything before you send the header. Make sure that you don't echo or print anything before and it should work as expected.

    Jean-Luc
     
    Jean-Luc, Mar 23, 2008 IP
  4. RoscoeT

    RoscoeT Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Or you could make your php code output some javascript with a document.location type redirect. Ugly but it would work the way you're trying to use it.
     
    RoscoeT, Mar 23, 2008 IP
  5. aminemx

    aminemx Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you can use this fucntion
     
    aminemx, Mar 23, 2008 IP
  6. LimeBlast

    LimeBlast Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I personally had a lot of problems with that and I discoverd that you need to put this command on the top of the page (line 1 of code or so)!
     
    LimeBlast, Mar 23, 2008 IP
  7. lektrikpuke

    lektrikpuke Well-Known Member

    Messages:
    297
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    113
    #7
    Can't put it at the top, because I want the page to reload if the form is not filled in correctly.
     
    lektrikpuke, Mar 24, 2008 IP
  8. lawrenz

    lawrenz Peon

    Messages:
    246
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    try puting location.replace('index.php'); or any file you want. =)
     
    lawrenz, Mar 24, 2008 IP
  9. jitendraag

    jitendraag Notable Member

    Messages:
    3,982
    Likes Received:
    324
    Best Answers:
    1
    Trophy Points:
    270
    #9
    I personally think it would be better if you actually output the form again and print an error that the condition is not met !! it would also be better if this 'new' form also has some of the entries user already did.

    you already have an answer for Location header not working :)
     
    jitendraag, Mar 26, 2008 IP