3

Possible Duplicate:
Headers already sent by PHP

Below is a simple example of my PHP code which (I hope so) is self explanatory. What I try to do is to update the session variable. But the output of the script is as follows:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /Library/WebServer/Documents/facebook/test.php:8) in /Library/WebServer/Documents/facebook/test.php on line 11

The warning is caused by the echo statements in line 8 and 9, of course. Is there any simple solution to stop this warning.

Thanks for any pointers, Andrej

<?php
session_start();
$_SESSION['percent'] = 0;
$iterations = 50;

for ($i = 0; $i <= iterations; $i++) {
  $percent = ($i / $iterations) * 100;
  echo "Hello World!";
  echo "<br />";
  // update session variable
  session_start();
  $_SESSION['percent'] = number_format($percent, 0, '', '');
  session_commit();
}
?>

The only solution that works (i.e. updates the session variable) for me is:

<?php
ob_start();
session_start();
$_SESSION['percent'] = 0;
$iterations = 50;

for ($i = 0; $i <= 50; $i++) {
  $percent = ($i / $iterations) * 100;
  echo "Hello World!";
  echo "<br />";
  // update session variable
  session_start();
  $_SESSION['percent'] = number_format($percent, 0, '', '');
  session_commit();
}
ob_flush();
?>

It's ugly, while it buffers the output first...

Community
  • 1
  • 1
Andrej
  • 3,719
  • 11
  • 44
  • 73

5 Answers5

4

Remove the session_start() from inside the for loop.

Put the session_commit() outside the for loop at the very end.

Both these functions should only be called once in a script.

James
  • 3,265
  • 4
  • 22
  • 28
  • 1
    Altho, you are overwriting the same session variable. What are you actually trying to do? – James Dec 24 '10 at 00:04
  • Reproduce this (look at process.php file): http://q2interactive.net/view/how-to-build-an-ajax-progress-bar-with-jquery-and-php.html – Andrej Dec 24 '10 at 00:29
  • Ahh, that was my guess. If you are trying to reproduce that then take out the echo statements. – James Dec 24 '10 at 00:35
  • session_start() needs to send a HTTP header to send a cookie. It can't do that if any other content has already been sent. So your only 2 answers are 1) take out the echo statements or 2) use output buffering, as you noted. – James Dec 24 '10 at 00:38
  • The idea behind my code is to print the elements of an array (not included in the example above) and show the progress using the progress bar. – Andrej Dec 24 '10 at 00:44
  • Well if you absolutely must print output as you go you can't use PHP sessions. Look to storing the progress variable elsewhere, like in a DB or a common memory cache or a file or something. Altho this must be a really really big array if it needs a progress bar just to print, are you sure this whole approach is the right one? – James Dec 24 '10 at 00:48
2

If you don't have any previeus output before the session-start() statement,then try to resave your .php file as an ansi-file or as utf-8 without BOM file. that's bc in some cases the editor itself writes things as an output in the file.

It worked for me

dudi
  • 29
  • 1
2

It's not possible to set cookies (or send any other headers) after output is started. You could add ob_start() at line 1 to buffer the output.

The right solution is to separate logic from the output. Check out e.g. http://www.paragoncorporation.com/ArticleDetail.aspx?ArticleID=21

b7kich
  • 4,253
  • 3
  • 28
  • 29
1

You have to execute the session_start() function only once. So just drop the one within the loop.

Also you don't have to do the session_commit() manually, in most cases PHP handles this for you automatically.

svens
  • 11,438
  • 6
  • 36
  • 55
1

As the others have stated, the cause of the error is the second session_start() you are using. However, the actual reason it's throwing an error is because you are trying to set a header after you've already sent output. Since the session_start() function sets the session cookie, it tried to set the cookie header, which is after you already echo content.

Sarah
  • 513
  • 4
  • 11