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 7609585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:12:28+00:00 2026-05-31T01:12:28+00:00

Please, explain how session_start() function works. I don’t understand the sequence of actions when

  • 0

Please, explain how session_start() function works.

I don’t understand the sequence of actions when starting a session in php. Try to explain.

HTTP is a client – server architecture. It means that the browser sends its request, the sever processes the request and sends back its answer. Each of these actions has appropriate headers.

I checked (with headers_list()) what headers are sent back by the server with its answer when I want to start session. And among others there is header

Set-Cookie: PHPSESSID=7f4cbf53fbcd4717792447f32da7dba8

It seems that everything is ok, the server gives order to the browser to set the cookie.

But.To start session I have to include the session_start() function in the beginning of the code of the page. So this function is started when the browser begins parsing the page. The browser meets the php opening tag <?php followed by the session_start() function. Immediately it delegates control to the server. And the server only now starts the function. Only when it has already sent the page to the browser with all the headers.

So I don’t understand how server can send Set-Cookie header before the browser starts parsing the page and meets the session_start() function? How it knows that it has to put Set-Cookie header before the command session_start() is executed? Or I misunderstand the process?

  • 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-31T01:12:29+00:00Added an answer on May 31, 2026 at 1:12 am

    I’ll answer by using Apache as the server.

    When a page is requested to the server, Apache routes it to the proper document.

    If that particular document type is associated with a SAPI, control is relegated to that SAPI. In case of PHP scripts, this would usually be PHP’s Apache module.

    Your PHP is then executed by the SAPI. When you call session_start(), PHP does a few things:

    • It checks if the client sent a cookie named after session_name(). That cookie contains the session ID. If it doesn’t exist, it creates it with a new session ID.

    • It loads the session data associated to that session ID from the session provider (the default provider stores session data in the temp folder of your server as a file containing serialized session data) and makes it available in the $_SESSION super global.

    • It registers a shutdown callback to save the session data back through the provider.

    Then your script continues its normal execution flow, the output being sent back to Apache to be sent to the client.

    Sessions are completely handled by the SAPI. Other than relegating control to the SAPI, Apache does nothing.

    Most PHP installations have output buffering on, which stores all output (headers and body) into a buffer till it is flushed to Apache (either explicitly through ob_flush() or implicitly at the end of your script).

    If output buffering is on, you may call session_start() anywhere before the first flush since PHP will send headers before the body.

    In the case of a new session (when the cookie is sent), it doesn’t matter if the client receives the session ID only after page execution: it will have it for the next request. The session ID is the key for restoring your session data. If someone steals your session ID and you do not have server-sode checks, your session data will be compromised.

    For more information, feel free to read another one of my answers.

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

Sidebar

Related Questions

Please explain what is name mangling, how it works, what problem it solves, and
i've a problem with php session handling that i can't explain to myself. I'm
Can someone please explain the difference between ViewState and Session? More specifically, I'd like
Please anyone explain me the difference between ViewState,Application and Session of a Page ?
I would like to know the difference between Session.clear(); Session.Abandon(); Session.RemoveAll(); Please explain the
Please forgive me if I don't explain this very well. First off, I am
Can someone please explain to me once and for good what are Session Key
I'm trying to get some service (e.g. session) from outside of controller. Please, explain
Please explain to me why the very last echo statement is blank? I expect
Please explain from Linux, Windows perspectives? I am programming in C#, would these two

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.