Pregunta

Ok, so I am working on this login system and of course when the user logs in i regenerate the session id. But after I regenerate my session id i also want to set a token to be stored in a cookie. However I seem to not be able to do that on the same page. I get an error that says:

Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /htdocs/somesite/test.php on line 44

Warning: Cannot modify header information - headers already sent by (output started at //htdocs/somesite/test.php:76) in /htdocs/somesite/test.php on line 54

This what I am doing right now:

session_regenerate_id();
setcookie("Foo","Bar", time()+$CookieExpireTime,"/");

I am wondering if there's a work around this. It's working when I'm testing it on localhost, but not on the server...

¿Fue útil?

Solución

Common Cause: This warning is often caused by a blank space or extra line at the beginning or end of a .php file. Check the error for the filename that generated the error (ie: the "output started at...." filename), open that file in your text editor and remove the extra spaces or lines immediately before the first

Other Causes: Syntax errors In the example above, you'll see output started at /....includes/something/something/something.php:12. The includes/something/something/something.php is the filename you need to be concerned about. The :12 means that the problem you need to fix is on line 12. In that same example, the "in /includes/something_else.php on line 67" message can be completely ignored. It is not the problem. It is the one that discovered that the problem had already occurred.

If the "headers already sent" error appears AFTER any other error, then you need to fix that other error FIRST. (The error message itself is what caused headers to be sent, so fixing that error will cause the second error to go away too.)

If the "started at" refers to line 1, then it's either a space before the opening <?php tag or it's incorrect encoding on the file. Remember, if your language is in UTF8, then you MUST encode the file as "UTF8-without-BOM", else the BOM (byte-order-mark, an invisible character at the beginning of the file) will cause this same headers-already-sent error.

Summary:

a) look for where it 'started at'

b) track the line number

c) check what's normally happening on that line.

  • If it's the end of the file, then it's blank spaces.
  • If it's the start of the file, it's likely spaces or incorrect encoding.
  • Elsewhere it could be a syntax error or the result of an "echo()" statement which is displaying info or perhaps debug code.
  • Common syntax errors include the use of single-quotes inside statements that already have single-quotes. Check to be sure your quotes aren't mismatched. If you need to use single-quotes while inside other single-quotes, change yours to \' instead of just '.

d) the rest of the info simply shows other execution information, mainly the part of the code that discovered that it cannot proceed as expected due to the problem that happened in the 'started at' location.

Source: zen-cart.com

Another similar thread on SO: How to fix "Headers already sent" error in PHP

I hope this helps.

Otros consejos

The problem isn't with the two functions, the problem is that you cannot call session_regenerate_id() once any output has been sent to the page. That function, like header() has to be called before any sort of output is sent to the page.

Either re-structure your page so that this function is called before any output is sent, or use output buffering so that this function is called before any page content is sent.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top