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):
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.
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:
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
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!
<?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; ?>
Header information can only be modified before any text is sent to the page. So removing the print statement should do the trick .
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.
What is confusing. It ensures that the headers are sent first, before any other output. It is clear as day to me.
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().
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.
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?
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.