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

Avatar of undefined
Last Comment
andy7789

8/22/2022 - Mon
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

www-i-p-mu

Hi andy7789,

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

Regards

www.i-p.mu
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?
Your help has saved me hundreds of hours of internet surfing.
fblack61
stormist

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
stormist

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
andy7789

ASKER
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!
stormist

Yep I understand, I was driven nuts by this error when transferring one of my scripts to a new server. Glad to help.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
TeRReF

It's probably because sesssion_auto_start was set to On in php.ini
andy7789

ASKER
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)
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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
TeRReF

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

ASKER
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?
andy7789

ASKER
TeRReF,
Note that sesssion_auto_start is Off for the both versions
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
TeRReF

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

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

Always put session_start() at the very top of your page... That should do the trick
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
andy7789

ASKER
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()?
TeRReF

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

ASKER
=="" ?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
TeRReF

session_id returns an empty string if there is no session present
andy7789

ASKER
I mean just =="" not ==="
:)
TeRReF

=== 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...
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
andy7789

ASKER
OK, clear, I have never seen that syntax