Solved

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

Posted on 2014-10-03
13
179 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
  • 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
Industry Leaders: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
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.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 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