Link to home
Start Free TrialLog in
Avatar of breeze351
breeze351

asked on

session_start() getting error on new server.

I've had to switch to a new server and am getting the following error:

Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /home/langsyst/public_html/Lansco/index.php:14) in /home/langsyst/public_html/Lansco/index.php on line 15

The source code and the display of the source from the browser are attached.
index_code.php
index_src.txt
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi
Just put session start at the very top of the file:

 <?php
session_start();
header("Cache-control: private");
error_reporting(E_ALL);
ini_set('display_errors', 1);
?>
<!DOCTYPE html>

Open in new window

I don't know how your script is, but if you don't need the doctype, if it is just a script which receives data and returns them after having processed, just remove doctype. If instead you have html markup after a php section do what I showed above: first php, then html.
Your version should not have worked on any server with PHP that I have used.  Ray's version is correct and Marco's code is the same thing but shorter.  Except I'm not sure that HTML comments are 'valid' when they are outside the <html> tags.  There have been issues with some versions of IE when you have content before the DOCTYPE and/or <html> tags.  You may say that it has worked before and I will say that you probably didn't check it.
@Dave: It's unusual to find this kind of setup "in the wild," but you can set output_buffering to be on, by default.  In a test environment, we probably want it off because we may want to see incremental output as the script runs.  But for a deployed application is makes good sense to turn it on.  When output buffering is on, the PHP script does not connect and disconnect from the web server repeatedly to send small packets of data -- it does it all at once at the end of the script.  This usually means better performance!  You can set output_buffering on in php.ini.

Also, I think the comments are benign, but it makes me itch to see them outside of the HTML document.  I usually put them after the doctype declaration.
I've seen a couple of cases where the programmer / web designer wanted to put comments ('credits') as the first lines on a page.  If I had to redo the page, they got moved.  Or deleted.
Avatar of breeze351
breeze351

ASKER

I ended up starting the page as follows:
<?php
//comments
session_start();

I wanted to be the comments at the top of the source code page
I ended up starting the page as follows:
<?php
//comments
session_start();

I wanted to be the comments at the top of the source code page