-2

Here is my simple PHP program:

<HTML>
<HEAD>
<TITLE>Lookout World!</TITLE>
</HEAD>
<BODY>
<?php
   session_start();
   echo "Hello, world!";
?>
</BODY>
</HTML>

When I run this PHP program through Apache 2.4 web server, it throws these errors:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at C:\Apache24\htdocs\Hello.php:6) in C:\Apache24\htdocs\Hello.php on line 8

Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at C:\Apache24\htdocs\Hello.php:6) in C:\Apache24\htdocs\Hello.php on line 8 Hello, world!

The weird thing is, is that if you just have this:

<?php
   session_start();
   echo "Hello, world!";
?>

... The program works just fine then, and there are no errors. But, for the software I am working on, we need to have the PHP embedded into HTML files. (For a school project that is). So, how do I fix these and get rid of these errors. Because in our school project, it gives these errors too, and we cannot log into our school project's PHP program.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Erik343
  • 323
  • 4
  • 14
  • 1
    The code you've posted works. It is kind of the answer you're looking for isn't it. You haven't posted code that doesn't work. I can only assume you are outputting something before session_start() which is exactly what the error says you did.... so just don't do that. – braks May 04 '15 at 02:56
  • Sorry, man, my code I typed in the question did not show up for some reason... – Erik343 May 04 '15 at 02:59

1 Answers1

0

Simply move your session_start() call to the top.

<?php
   session_start();
?>
<HTML>
<HEAD>
<TITLE>Lookout World!</TITLE>
</HEAD>
<BODY>
<?php
   echo "Hello, world!";
?>
</BODY>
</HTML>

Note: To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

http://php.net/manual/en/function.session-start.php

braks
  • 1,505
  • 15
  • 23