3

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

DreamWave
  • 1,934
  • 3
  • 28
  • 59

6 Answers6

8

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
  • 23,820
  • 10
  • 76
  • 86
  • 3
    Have you checked for BOM and auto-prepend? – symcbean Oct 21 '10 at 12:44
  • 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 ` – Matt Gibson Oct 21 '10 at 12:49
  • 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 Oct 21 '10 at 12:51
  • @DreamWave: those are standard PHP headers, they are OK. You must see what is on the files mentioned in the errors! – Palantir Oct 21 '10 at 13:17
  • @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 Oct 21 '10 at 13:36
6

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.

ruben
  • 1,745
  • 5
  • 25
  • 47
  • there is nothing - as I said - these are the first 5 lines of my index.php file – DreamWave Oct 21 '10 at 12:45
  • Something is definitely being sent to the browser. What happens if you call this echo (headers_sent())?'sent':'not sent'; just before you call the session_start(); – ruben Oct 21 '10 at 12:53
  • I get "sent" .. how can I check what is sent ? – DreamWave Oct 21 '10 at 12:58
  • 1
    What if you temporarily replace the index.php that's there at the moment with a simple one that _only_ says ``? Do you get the same result? Or does it say "not sent"? – Matt Gibson Oct 21 '10 at 13:03
  • I am not sure but ob_clean() may clean the output buffer, give it a try using immediately before session_start(); – ruben Oct 21 '10 at 13:19
3

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
  • 5,538
  • 2
  • 20
  • 15
2

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
  • 10,163
  • 2
  • 29
  • 30
  • 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 Oct 21 '10 at 12:55
  • It was tested on a PHP 4 box that is six years old. – Andrew Sledge Oct 21 '10 at 13:05
  • It wouldn't cause any problems if output buffering is on, and that's configuration dependant. – Mauricio Oct 21 '10 at 13:25
1

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
  • 387
  • 1
  • 11
0

I had this problem caused by two different things:

1- there were an empty line at the beginning of the file case the php 5.2 to push 'headers already sent' warning.

http://board.phpbuilder.com/showthread.php?10310794-RESOLVED-Warning-session_start()-Cannot-send-session-cookie-headers-already-sent

if < ?php is at line 2 of your script and a blank line is above it then that can cause problems such as the php header function not working.

2- PHP output_buffering was turned off by default. If you're using shared hosting and code works on localhost maybe this can be the reason. just set these parameter in php.ini or local .user.ini

output_buffering=4096

M at
  • 1,020
  • 1
  • 12
  • 26