How do I resolve the "Cannot send session cookie" php error?

I am getting an annoying php error. I have googled this but none of the suggestions work for me. It works running locally but fails on a remote server I'm trying to run it on.

I have simplified the code to just this:

<?php
    session_start();
    echo 'Current PHP version: ' . phpversion();
?>



On my server it outputs:

Current PHP version: 5.4.2411



On the remote server it outputs:

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home3/dekel/public_html/andromedapedasi.com/test.php:3) in /home3/dekel/public_html/andromedapedasi.com/test.php on line 5

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home3/dekel/public_html/andromedapedasi.com/test.php:3) in /home3/dekel/public_html/andromedapedasi.com/test.php on line 5
Current PHP version: 5.2.1711


The version are slightly different, which may explain the discrepency. Anything I should check for? I have tryed without a php.ini file, and also with one containing:

session.auto_start = 0;
session.use_cookies = 1;


No joy in either case. What should I do or check for to get this working on the remote server? I don't need to enable sessions anywhere in phpMyAdmin or anything do I? Could some default settings be different between the machines?

Thanks
LVL 1
paddycobbettAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
Is that the whole page, the error indicates you have something before it that is sending output to the browser.
Also make sure the page is saved as UTF-8 without BOM
0
paddycobbettAuthor Commented:
Yes, that is literally the only content sitting in a file called test.php. I removed everything else thinking that this would surely work!? Is it possible that the server is sending something extra somehow?
0
GaryCommented:
Can you attach the file here
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

gr8gonzoConsultantCommented:
1. Make sure the file mentioned in the error message is the same file you're testing.
2. If you have ANY whitespace before that first <?php (even blank lines), remove them so that the VERY FIRST thing in your file is <?php
0
paddycobbettAuthor Commented:
Sure, like I said it works on my local machine, but not the remote machine. Thanks for looking in to this =)
0
paddycobbettAuthor Commented:
Can i call session_start(); freely on each page request? i.e It's not only supposed to be called once per session or anything?
0
GaryCommented:
It needs to be at the start of every page where you use sessions.

Are you attaching the file?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gr8gonzoConsultantCommented:
Try adding the file path below your session_start() and testing it on the remote server, like this:

<?php
    session_start();
    echo __FILE__ . "<br />\n";
    echo 'Current PHP version: ' . phpversion();
?>

Open in new window


Tell us the output of that.
0
gr8gonzoConsultantCommented:
The reason it might work different between your servers could be related to something called output buffering. Basically, output buffering means that the content of your script isn't -immediately- sent from the server to the person who's loading the page. Instead, that content gets stored into memory and then sent to the person at a later time (usually when the script completely finishes running). This is basically a requirement if your server does any on-the-fly page compression. So your local server might be doing output buffering, while the remote server is not.

A side effect of output buffering is that you can modify the headers (something that session_start also does) at any time until the output gets sent. PHP basically sees a request to change a header and checks to see whether anything has been sent to the client yet. If not (e.g. output buffering or compression is turned on), then it updates the headers.

So your script might have some whitespace or content before the <?php part and your local server is simply okay with that because of how it is configured.
0
Dave BaldwinFixer of ProblemsCommented:
If it still at 'andromedapedasi.com/test.php' , it appears to be working.  I'm not seeing any error messages.
0
Ray PaseurCommented:
You'll want to upgrade the remote server.  PHP 5.2 is not supported any more, not even for security fixes.

See if this article helps.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_4423-Warning-Cannot-modify-header-information-headers-already-sent.html
0
paddycobbettAuthor Commented:
Yes Dave, it seems to be working now!? Thanks for all the comments, it's helped me understand a few more aspects of php since i'm still a newbie. My only idea of why it could have suddenly started working is maybe the php.ini didn't kick in straight away where i set cookies to be on, no idea. Thanks for all the responses!
0
paddycobbettAuthor Commented:
Thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.