I can't create cookie!

Warning: Cannot modify header information - headers already sent by (output started at C:\AppServ\www\demo\test.php:2) in C:\AppServ\www\demo\test.php on line 3
I see this error because before that have the echo command ,I think so. Can you help me fix this error ???
My English isn’t good,hope everybody sympathize for me ? Thanks you !

Can you show us the code

You’re already identified the problem - you have an echo before you try to create the cookie. You either have to move that echo so that nothing at all - not even any white space in the html code - is sent before the headers, or look at output buffering - http://php.net/manual/en/book.outcontrol.php

1 Like

Thanks droopsnoot,I think i just added to my code the command ''ob_start();" and “ob_end_flush();” will be all right ,it make the brower doesn’t show the error but still running the commands !

ob_start and ob_flush should only be used in development, but not in a final release. If all you’re concerned about is forcing the error to not be displayed on screen then you have a lot of problems coming to you. By ignoring the errors, you might not actually see other errors that are being complained about after or before the headers are being sent.

It is strongly suggested that you post your codes here so we can take a look at it and debug what you have.

What must I do ? If don’t use this way then we can’t create cookie with an error display on the screen .
I haven’t learnt javascript so can’t do that and sounds bad for my website !

There are several possibilities:

  • you actually create output before sending the headers (an echo statement or html code for example)
  • you have some kind of whitespace (an empty line at the end of a php script after the closing ?> tag fro example)
  • you saved the files with a BOM

But to be sure, and to help you find the exact reason of the error, you’ll need to show us your code.

There are more ways to do something. If you could just kindly show us your code, we can help you figure out what the problem is and fix the problem for you so you don’t have to use your last resort which is ob_start and ob_flush.

Whatever you are currently outputting before setting the cookies should either not be output at all or should be saved to be output after all the headers are sent.

You want maximum error reporting on in development so you can actually fix all the errors properly before the code goes live.

Using ob_start and ob_flush is almost always completely unnecessary (I don’t know why they even added such useless commands to the language as I have never found a situation where they are needed).

var_dump() into a variable?

I suppose they might be useful for testing if you have issues with the part of the code that outputs the headers although I usually find that when testing the code that outputs the headers I don’t want the Location header transferring to a different page so that I don’t get to see the output from the var_dump()

I think if the intent is to var_dump to a string, var_export would be the better choice

Thanks everybody,I received comments you take out ! I will fix and update my source in order to them can be more brief . Today I’m a newbie but in one day I will be a web developer and I won’t forget your help !

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.