0

Possible Duplicate:
Headers already sent by PHP

The following code in PHP gives the warning.

<?php
    if(!isset($_SESSION))
    {
        session_start();  // line 4
    }

    if(!isset($_SESSION['valid_admin']))
    {
        header("location:Login.php");  //line 9
    }
?>

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at C:\wamp\www\wagafashion\Order.php:8) in C:\wamp\www\wagafashion\Lock.php on line 4

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\wamp\www\wagafashion\Order.php:8) in C:\wamp\www\wagafashion\Lock.php on line 4

Warning: Cannot modify header information - headers already sent by (output started at C:\wamp\www\wagafashion\Order.php:8) in C:\wamp\www\wagafashion\Lock.php on line 9

I searched on Google and found a question here but I by far don't understand what is mentioned there. How could I solve it?

[My application with the above code was working fine but a few days ago the system crashed with the blue screen error and I had to reinstall it where I installed a different version of WAMP. Is this the problem?]

Community
  • 1
  • 1
Tiny
  • 27,221
  • 105
  • 339
  • 599

4 Answers4

2
<?php
    session_start();

    if(!isset($_SESSION['valid_admin']))
    {
        header("location:Login.php");  //line 9
    }
?>

Session will never be set in that if statement unless you call session_start()

If you call session_start() before the if statement (like you should be doing) your condition will always be true, rendering the if statement useless.

So you can place session_start() above the first if statement and then delete the first if statement.

Max Hudson
  • 9,961
  • 14
  • 57
  • 107
  • Thank you but I already tried that. It also ends with the same warning. Actually, that `if(!isset($_SESSION))` condition was not there in my original project. I have just tried it to see if it makes a difference. – Tiny Jul 11 '12 at 23:59
  • this code must be placed before ANY html for it to work. please include the entire document in your OP and place the code I posted at the very top of it. – Max Hudson Jul 12 '12 at 00:01
  • The file contains the only code mentioned in the question. – Tiny Jul 12 '12 at 00:04
  • is it included in another document? – Max Hudson Jul 12 '12 at 00:05
  • Yes, It's included with all the pages of my admin panel so that if anyone attempts to open a page without login, he/she is redirected to the login page. My confusion is that i was working previously before the system crashed. – Tiny Jul 12 '12 at 00:10
  • and is it included after html? please post one of the pages it is included in? – Max Hudson Jul 12 '12 at 00:12
  • I put the `include(...)` statement on the very first line of one of pages of admin panel and it worked without any warning. Could you please tell me why was it working before? Just for the reason that I will have to make so many changes in my project. It's a very big project. – Tiny Jul 12 '12 at 00:20
  • The reason I could see is `output_buffering` in my php.ini file. It was `off` by default. I turned it on like `output_buffering = on` and the thing happened as I expected. Thank you. – Tiny Jul 12 '12 at 01:28
2

You can also use http://php.net/manual/en/function.ob-start.php function at the beginning of your script.

mrok
  • 2,680
  • 3
  • 27
  • 46
1

First guess, check for whitespace output to the browser that isn't immediately visible to the eye.

davethegr8
  • 11,323
  • 5
  • 36
  • 61
1

It seems that you misunderstand the session_start() function:

session_start — Start new or resume existing session

There is no need to check if it exists first.

if(!isset($_SESSION['valid_admin'])){
    header("location:Login.php");  //line 9
} 

Note:

As of PHP 4.3.3, calling session_start() after the session was previously started will result in an error of level E_NOTICE. Also, the second session start will simply be ignored.

Based off of your warning messages, you have two files: Order.php & Lock.php. Which one of those files is your posted code located in? Having a file included can cause conflicts due to output already existing.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called.

Robert
  • 8,717
  • 2
  • 27
  • 34
  • Thank you but I already tried that. It also ends with the same warning. Actually, that `if(!isset($_SESSION))` condition was not there in my original project. I have just tried it to see if it makes a difference. – Tiny Jul 12 '12 at 00:04
  • Please see my edit and offer additional details. – Robert Jul 12 '12 at 00:05