Solved

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

Posted on 2014-10-03
13
164 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
Comment Utility
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
Comment Utility
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
Comment Utility
Can you attach the file here
0
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 58

Accepted Solution

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

Are you attaching the file?
0
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
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 34

Expert Comment

by:gr8gonzo
Comment Utility
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 82

Expert Comment

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

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now