paddycobbett
asked on
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/a ndromedape dasi.com/t est.php:3) in /home3/dekel/public_html/a ndromedape dasi.com/t est.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/a ndromedape dasi.com/t est.php:3) in /home3/dekel/public_html/a ndromedape dasi.com/t est.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
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/a
Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home3/dekel/public_html/a
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
ASKER
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?
Can you attach the file here
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Sure, like I said it works on my local machine, but not the remote machine. Thanks for looking in to this =)
ASKER
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try adding the file path below your session_start() and testing it on the remote server, like this:
Tell us the output of that.
<?php
session_start();
echo __FILE__ . "<br />\n";
echo 'Current PHP version: ' . phpversion();
?>
Tell us the output of that.
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.
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.
If it still at 'andromedapedasi.com/test. php' , it appears to be working. I'm not seeing any error messages.
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.
https://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_4423-Warning-Cannot-modify-header-information-headers-already-sent.html
See if this article helps.
https://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_4423-Warning-Cannot-modify-header-information-headers-already-sent.html
ASKER
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!
ASKER
Thanks!
Also make sure the page is saved as UTF-8 without BOM