Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7014547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:32:07+00:00 2026-05-27T22:32:07+00:00

I just noticed something strange. I thought that, as PHP’s manual says, session_start() must

  • 0

I just noticed something strange. I thought that, as PHP’s manual says, session_start() must be called before any output is sent to the browser:

To use cookie-based sessions, session_start() must be called before outputing anything to the browser.

So, just for curiosity, I’ve created two scripts. One is write.php:

<?php
echo 'foo';

session_start();
$_SESSION['bar'] = 'baz';
?>

And the other one is read.php:

<?php
echo 'foo';

session_start();
var_dump($_SESSION['bar']);
?>

And surprisingly, the session is written and read even after echoing foo.

However, if I add a call to flush() after the echos, Apache’s error log reports:

[Tue Jan 03 11:57:21 2012] [error] [client 127.0.0.1] PHP Warning: session_start(): Cannot send session cache limiter – headers already sent in /var/www/sessions/write.php on line 5
[Tue Jan 03 11:57:21 2012] [error] [client 127.0.0.1] PHP Stack trace:
[Tue Jan 03 11:57:21 2012] [error] [client 127.0.0.1] PHP 1. {main}() /var/www/sessions/write.php:0
[Tue Jan 03 11:57:21 2012] [error] [client 127.0.0.1] PHP 2. session_start() /var/www/sessions/write.php:5

So, my questions is: why is the session written correctly after echoing something? Isn’t it immediately sent to the browser? And, if so, does it mean I can start the session anywhere, as long as I don’t call flush() before?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:32:08+00:00Added an answer on May 27, 2026 at 10:32 pm

    To use cookie-based sessions, session_start() must be called before
    outputing anything to the browser.

    That’s true. Server-side side cookie setting (unlike JavaScript cookie setting) works by sending an HTTP header. HTTP headers go before the actual document: once you start sending the document, there’s no place for further headers.

    In your case, what happens is that this line:

    echo 'foo';
    

    … does not actually send output to the browser. Instead, it adds some output to a queue that will be sent later. The PHP interpreter is configured to hold this output until certain event happens (possibly, the scripts ends or the queue reaches certain size).

    The output_buffering directive is the likely suspect.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just noticed something that looks strange to me. Visual C++ doesn't align object in
I just noticed something strange. If I have this XML: <level number=7 background=background_5> and
Let me start from a real life example: Customer: Alex, just noticed something strange
Possible Duplicate: Puzzling Enumerable.Cast InvalidCastException Hi, I just noticed something quite strange with the
I just noticed that java.beans.Introspector getBeanInfo does not pickup any superinterface's properties. Example: public
i have just noticed something strange in some asp.net markup. I have a standard
I've just noticed that something terrible is missing in jquery-ui. A way to rotate
I have been doing spring cleaning in my app. I noticed something strange, that,
I just started using xdebug to profile my application and immediately noticed something strange
While inspecting ArrayList API, I noticed something that looks strange. Indeed, here the the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.