Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • Last Modified:

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
0
paddycobbett
Asked:
paddycobbett
  • 5
  • 3
  • 3
  • +2
2 Solutions
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now