what is wrong with this code?

Discussion in 'PHP' started by kks_krishna, May 16, 2007.

  1. #1
    I am getting the following error:
    Warning: Cannot modify header information - headers already sent by (output started at /home/content/k/k/s/kkskrishna/html/

    what is wrong?

    <?php 		
    		session_start();
    		session_register('USER_NAME');
    		$_SESSION['USER_NAME'] = $_GET['user_name'];
    		print($_GET['user_name']);
    		print($_SESSION['USER_NAME']);
    		header("Location: http://www.domain.net/templates/put_ses.php");
    		exit;
    ?>
    Code (markup):
     
    kks_krishna, May 16, 2007 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Just to be sure... are you certain that message is coming from that file and not the file that you are being redirected to?

    Anyway, I have to guess that that isn't the complete error message. What's happening is the classic 'you sent output then tried to send a header', which you can't do.

    Looking at the complete error message you will see exactly where that output is happening. 'Output' can include whitespace, so make sure there is NOTHING outside of the <?php and ?> tags (even a new line character after ?> counts as whitespace!).

    So yeah, look at that error message and it will tell you where the problem is. Get rid of the output and you're fine.
     
    TwistMyArm, May 16, 2007 IP
  3. technoguy

    technoguy Notable Member

    Messages:
    4,385
    Likes Received:
    308
    Best Answers:
    0
    Trophy Points:
    205
    #3
    Check if any blank line at the top and remove left spaces near session_start

    thanks
     
    technoguy, May 16, 2007 IP
  4. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Pretty much exactly as the error messages states - you can't send headers after any output. Remove the print lines because in this instance they don't do anything - what are you expecting to happen? Either you show a page or you redirect somewhere else, you can't do both! (well, at least not using header locations)

    <?php 		
    		session_start();
    		$_SESSION['USER_NAME'] = $_GET['user_name'];
    		header("Location: http://www.domain.net/templates/put_ses.php");
    		exit;
    ?>
    PHP:
     
    rodney88, May 16, 2007 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    LOL. I'm an idiot when I haven't had my coffee... here I am talking about whitespace and so on and totally missed the fact that he was printing right there in the code :)

    My point still stands, though: you copied half an error message for us... unless you're paying your ISP by the number of bits you upload, there's a simple rule when you're asking for help on a forum: tell us EVERYTHING you know. Half an error message is less than half as useful :)
     
    TwistMyArm, May 16, 2007 IP
  6. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Rodney88 got the answer you need. Also, if you are using the $_SESSION array directly (you should) there is no need to use session_register. Session_register depends on register_globals being turned on and best practice is to always leave register_globals off, which will be mandatory in PHP 6. Register_globals is an early design flaw which PHP has outgrown, don't use it!
     
    lemaitre, May 16, 2007 IP
  7. bibel

    bibel Active Member

    Messages:
    289
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #7
    <?php
    ob_start();
    session_start();
    session_register('USER_NAME');
    $_SESSION['USER_NAME'] = $_GET['user_name'];
    print($_GET['user_name']);
    print($_SESSION['USER_NAME']);
    header("Location: http://www.domain.net/templates/put_ses.php");
    ob_end_flush();
    exit;
    ?>
     
    bibel, May 17, 2007 IP
  8. Felu

    Felu Peon

    Messages:
    1,680
    Likes Received:
    124
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Header information can only be modified before any text is sent to the page. So removing the print statement should do the trick :).
     
    Felu, May 17, 2007 IP
  9. Mnemonic

    Mnemonic Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    bibels solution would be more appropiate i think
     
    Mnemonic, May 17, 2007 IP
  10. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Mnemonic: why is that, exactly? The output buffering will stop the output but it does not stop the sending of headers. The end result is exactly the same as not printing anything in the first place and all that ends up happening is the coder wonders why he doesn't see his output before being redirected...

    What I'm trying to say is, it actually confuses the situation, as far as I'm concerned.
     
    TwistMyArm, May 17, 2007 IP
  11. bibel

    bibel Active Member

    Messages:
    289
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #11
    What is confusing. It ensures that the headers are sent first, before any other output. It is clear as day to me.
     
    bibel, May 17, 2007 IP
  12. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #12
    1]Remove white spaces if any on top of the page
    2]Remove the print commands
    3]If you need that print statements to be included, use ob_start() and ob_end_flush().
     
    tamilsoft, May 18, 2007 IP
  13. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #13
    It's a very long way round - all he wants to do is redirect to another page. Turning on the output buffer is just extra overhead. If the script was a bit more complex, or even if we simply ever wanted to see the output, you could justify using the ob but we don't. We only need to send that header and whatever may be printed after that will never get through because we've already been sent to another page.

    Or to put it another way, effectively you're using the ob_ functions to do the job of commenting out or removing two print commands.
     
    rodney88, May 18, 2007 IP
  14. Arson

    Arson Well-Known Member

    Messages:
    622
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    120
    #14
    why not just print a meta refresh and set the time to .01 if you cant get the header() to work right on the page?
     
    Arson, May 18, 2007 IP
  15. coderbari

    coderbari Well-Known Member

    Messages:
    3,168
    Likes Received:
    193
    Best Answers:
    0
    Trophy Points:
    135
    #15
    no one should echo/print before sending any header.the main problem in this code is print.you have ob_start() at the start and ob_end_flush() at the end of this script.
     
    coderbari, May 19, 2007 IP