-2

Possible Duplicate:
Headers already sent by PHP

I have php version 5.4.4

I have this code

echo "a";
session_start();
$_SESSION['login'] = "Jhon";
echo $_SESSION['login'];

and this works, why? where is error: "header already sent...." ???

Community
  • 1
  • 1
Oto Shavadze
  • 40,603
  • 55
  • 152
  • 236
  • What level of error logging have you got? – Nicholas King Sep 26 '12 at 09:27
  • Why it should show an error as long as you are not using header('something') ? – Shameer Sep 26 '12 at 09:31
  • that error doesn't appear only when using header.. it can appear also when starting session – Mihai Matei Sep 26 '12 at 09:32
  • @hakra, "why i obtain error" and "why I **NOT** obtain error" is different questions. You do not agree? – Oto Shavadze Sep 26 '12 at 09:46
  • Slightly, it's the inverse. I reviewed it, and the only difference I could find is that the canonical answer *does not list session auto-start* as one reason to prevent that. The output buffering you accepted as answer is given there. Keep in mind that asking why something does not work can quickly turn into an unhelpful question, e.g. you do not get an error message because a) the error did not appear or b) you have disabled error reporting. See the issue? – hakre Sep 26 '12 at 09:50
  • @OTARIKI Read the top (accepted) answer to the question linked as a duplicate. It covers your scenario too. – Leigh Sep 26 '12 at 09:50

3 Answers3

4

Simply put, output buffering is on, which will make sure your script will work like this. It's not advised to depend on that though, as other servers may not have output buffering on by default. If you wish to turn it off (and I would), change the output_buffering directive to 0 in your php.ini.

Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
  • Simply put, this question is a duplicate. You look like a more common SO user who could help to garden the PHP tag a little and highlight duplicates. – hakre Sep 26 '12 at 09:42
  • Next to output buffering, the session could have been started *already*. http://php.net/manual/en/session.configuration.php#ini.session.auto-start – hakre Sep 26 '12 at 09:46
  • @hakra It's actually the exact opposite of the question you've flagged it to be a duplicate with. He knows why it should give an error, but doesn't understand why it doesn't. You're right though; I should try to look for duplicates first, answer later ;) – Berry Langerak Sep 26 '12 at 09:47
  • I'd even go so far to say that something like the exact opposite can qualify as a duplicate ;) Also, you didn't mention that probably error reporting has been disabled :D. – hakre Sep 26 '12 at 09:52
  • +1: For offering help in dupe-search. – hakre Sep 26 '12 at 09:53
1

Once the output sent to the browser(even a space),you can not send any header
information to the browser.When you start a new session(using session_start)
a session cookie is sent to the browser from server and this cookie is
basically encapsulated inside the response header.

you can check if headers has already been sent using php function headers_sent()

Tarun
  • 3,162
  • 3
  • 29
  • 45
-1

Though your code worked well with me, but u can try using session_start() before echo statement.

AkiShankar
  • 332
  • 5
  • 16