Link to home
Start Free TrialLog in
Avatar of andy7789
andy7789

asked on

Session_start(): Cannot send session cache limiter warning

Hi X-perts,

I am migrating to another server with a different php configuration and getting this warning:

session_start(): Cannot send session cache limiter - headers already sent (output started ....

I have a main page where i start session and an iframe (the same page), which has another session_start() statement

My questions:

1. If I remove the session_start from the iFrame, I am not getting this warning, but my code is not functioning properly, because the iFrame cannot get session variables. What should I do?
2. Why did not I have this warning on the old php?
3. Maybe I should replicate exactly the same php.ini file?
4. What php.ini line could supress such a warning in my old php config?

Please, advise

Thanks

-A-
Avatar of stormist
stormist

Your problem is session_start(); must be the very first statement in your script. So the very top must read
<?php
session_start();

EVEN WHITESPACES can mess this up and cause this error. Some ppl to be safe do this: <?php session_start();

No need to suppress the warning, making it your first statement will fix this problem. As to why it didnt' do this in the old server, I found the same thing one time when migrating one of my scripts. It just seems to be either a different in the PHP versions or config settings. In any case, get in the habit of putting session_start() as your very first statement and you won't have this error in the future. It also should be pretty easy to change your code.

Cheers

Hi andy7789,

Could you elaborate your question, i don't fully understand your question?

Regards

www.i-p.mu
Avatar of andy7789

ASKER

It is actually on the top:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link href="mystyle_search.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php session_start();

Should I place it before html code?
Yes, there is your error. the very very first statement in the page, or you get the error. It must be before any HTML code or anything. Try it :)
ASKER CERTIFIED SOLUTION
Avatar of stormist
stormist

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, you are right! It makes the trick. Still cannot understand why I have never had it before - the same php 4.3
Thanks a lot!
Yep I understand, I was driven nuts by this error when transferring one of my scripts to a new server. Glad to help.
It's probably because sesssion_auto_start was set to On in php.ini
A great remark. what is the difference? Should I really change it to off? (probably in my old php.ini it was off - should take a look)
Avatar of blue_hunter
check the setting of your php.ini, regarding the configuration for the error handling
http://www.nyphp.org/phundamentals/error_handle.php  <---- this is the link for the references
No, I think it's off in your current php.ini and on in your previous one. Leave it off, it forces you to pay attention to your sessions.
I have compared my old and new php config -> the key difference in the session section:

The old one:
session.bug_compat_42 Off
session.gc_divisor     1000
session.use_trans_sid   Off

The new one:
session.bug_compat_42 On
session.gc_divisor     100
session.use_trans_sid    On

That's it. I have noticed exra troubles with session_start() placement. It has become so "sensitive"... What could cause this? Probably session.bug_compat_42?
TeRReF,
Note that sesssion_auto_start is Off for the both versions
Have a look at the display_errors key in php.ini, That might have been off in your old and on in your new php.ini. Or the error_level might be different...
TeRReF

You are right, in the new php.ini display_errors = On. However, it did not make it easier: if I change it to Off, I do not get any warnings, but the code is not functioning as should mainly because of the mess with sessions
Always put session_start() at the very top of your page... That should do the trick
What happens if I have a few session_start() on the page? I have a few include() statements and each may contain session_start.

So far, I did not have errors or warnings. Should I really clean the code and get rid of multiple session_start()?
No, you could use this:
Change the single session_start() line into:

if (session_id() === '')
    session_start();

You will have to do that for every session_start call yes... But this method will prevent you from cleaning too much which might cause no session to be started at all on some of the pages...
=="" ?
session_id returns an empty string if there is no session present
I mean just =="" not ==="
:)
=== means you chaeck for value and type... =='' will work as well... It's just a force of habbit for me to check as strict as possible...
OK, clear, I have never seen that syntax