tags:

views:

79

answers:

6

These are my first 5 lines in my index.php:

  <?php
session_start();
if (!isset($_SESSION['cart'])) $_SESSION['cart']='';
$page = $_GET['page'];
?>

and so on.. I'm looking at the sessions trough the firefox->firebug->firecookie plugin and a session is not created.(I am sure of it because the cart that worked yesterday doesnt work today.) Any ideas why this might happen and how to fix it ?

I found this when enabling errors: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/controll/public_html/metalkonsult.com/index.php:1) in /home/controll/public_html/metalkonsult.com/index.php on line 2

I explored further - something IS sent to the browser but I dont know where its coming from. I did a var_dump(headers_list()); on the first line and this is what I get:

array(2) { [0]=> string(23) "X-Powered-By: PHP/5.2.6" [1]=> string(23) "Content-type: text/html" }

How do I turn this off ? What's sending it ?

I set session.auto_start = 1 in php.ini in the website folder sessions work now.. dont know what caused the problems but problem is temporarily fixed

A: 

Are the server settings changed?

Peter van Kekem
I dont remember changing anything. I only have access to php.ini. What settings could have an impact on the sessions ?
DreamWave
+8  A: 

If the white space I see before your php opening tag (<?php) is in your source code too, then this is the reason :) Make sure that no output has been done before your session_start call.

You can check with firebug the exact output, by going to the net tab, locating your PHP file transfer, expanding it, and seeing the Response tab. You could put a print just after the session_start and see where it appears... it should be the very first thing.

Otherwise, if you have php errors directed to log, go and see the log if there is anything in it. Finally, make sure that the browser is displaying session cookies too: some browsers don't show them.

Palantir
No, the <?php is at byte 0
DreamWave
Have you checked for BOM and auto-prepend?
symcbean
This pretty much has to be the answer, from what I can see. Have you used a different text editor, converted the file encoding, anything like that? And when you say the `<?php` is at byte 0, how have you checked that, exactly?
Matt Gibson
I've been using the same editor I created it with.. how can I take care of the problem if that is the issue ?
DreamWave
@DreamWave Which editor?
Matt Gibson
Its called UltraEdit
DreamWave
@DreamWave: those are standard PHP headers, they are OK. You must see what is on the files mentioned in the errors!
Palantir
@DreamWave Just a quick check: In the UltraEdit [Unicode settings](http://www.ultraedit.com/support/tutorials_power_tips/ultraedit/unicode.html) under Advanced -> Configuration -> File Handling -> Save, are the settings to save the UTF-8 BOM checked?
Matt Gibson
no, they are not, should they be checked ?
DreamWave
A: 

Works for me. Make sure your server has write access to the folder where session data is kept (check your session path: http://php.net/manual/en/function.session-save-path.php)

      <?php
    session_start();
    $_SESSION['cart'] = "test<br />";
    print $_SESSION['cart'];
    if (!isset($_SESSION['cart'])) $_SESSION['cart']='';
    print $_SESSION['cart'];
//blank it out
    if (isset($_SESSION['cart'])) $_SESSION['cart']='';
    print $_SESSION['cart'];
    ?>

outputs

test
test

@Palantir: And the whitespace doesn't matter.

Andrew Sledge
It depends by PHP version probably, as modern ones usually detect this kind of problems and cut the empty space off. The same goes for trailing space after the php closure. But you know, when something bad happens it's best to eliminate all possible causes... For this same reason I don't use the php close tag at all whenever possible: why exposing yourself to trouble?
Palantir
It was tested on a PHP 4 box that is six years old.
Andrew Sledge
It wouldn't cause any problems if output buffering is on, and that's configuration dependant.
Mauricio
+5  A: 

Something is being sent to the browser first which is causing the headers to be sent. Check your code to make sure that there isn't even a single space before your PHP code.

newcomer
there is nothing - as I said - these are the first 5 lines of my index.php file
DreamWave
Something is definitely being sent to the browser. What happens if you call thisecho (headers_sent())?'sent':'not sent';just before you call the session_start();
newcomer
I get "sent" .. how can I check what is sent ?
DreamWave
What if you temporarily replace the index.php that's there at the moment with a simple one that _only_ says `<?php echo (headers_sent())?'sent':'not sent'; ?>`? Do you get the same result? Or does it say "not sent"?
Matt Gibson
it still says "sent"
DreamWave
I am not sure but ob_clean() may clean the output buffer, give it a try using immediately before session_start();
newcomer
no, it says there is nothing in the buffer
DreamWave
probably you already tried using another browser?
newcomer
A: 

Is that the first PHP page called or is it included from another one?

If it is the first, try enabling output_buffering on php.ini. You may then be able to see what is being sent before by calling ob_get_contents()

Mauricio
+1  A: 

Some editors add an invisible UTF-8 byte order mark at the beginning of the file. Depending on the various server software versions, it may or may not be sent to the browser. Could you check a hex dump of your source code and make sure the first 5 bytes are 3c 3f 70 68 70?

Pianosaurus