Solved

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

Posted on 2014-10-03
13
180 Views
Last Modified: 2014-10-03
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
Comment
Question by:paddycobbett
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 40360169
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
 
LVL 1

Author Comment

by:paddycobbett
ID: 40360225
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
 
LVL 58

Expert Comment

by:Gary
ID: 40360239
Can you attach the file here
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 250 total points
ID: 40360333
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
 
LVL 1

Author Comment

by:paddycobbett
ID: 40360334
Sure, like I said it works on my local machine, but not the remote machine. Thanks for looking in to this =)
0
 
LVL 1

Author Comment

by:paddycobbett
ID: 40360341
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
 
LVL 58

Accepted Solution

by:
Gary earned 250 total points
ID: 40360343
It needs to be at the start of every page where you use sessions.

Are you attaching the file?
0
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40360344
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
 
LVL 35

Expert Comment

by:gr8gonzo
ID: 40360360
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
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 40360399
If it still at 'andromedapedasi.com/test.php' , it appears to be working.  I'm not seeing any error messages.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40360406
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
 
LVL 1

Author Comment

by:paddycobbett
ID: 40360478
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
 
LVL 1

Author Closing Comment

by:paddycobbett
ID: 40360483
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question