Hey.

Question about the function session_start() in PHP . I understand that when the PHP interpreter sees the session_start() function, then in the absence of a session, a Set-Cookie:sessionid=... field is created Set-Cookie:sessionid=... in the header of the http server response. But the http header fields can only be created BEFORE the start of the data output in the server response body - before any characters (markup, spaces ...) appear in the body. It turns out that tags <? ?> <? ?> should go at the very beginning of the document (WITHOUT spaces before them), and inside <? ?> <? ?> Before any echo session_start() should go. Do I get it right?

    1 answer 1

    First, the session can not be stored in a cookie (but in order to use it, you need to use session.use_cookies , a search on this parameter will lead to other storage options), respectively, the restriction is removed that nothing needs to be output to the browser before it starts.

    Secondly, yes, you need to start a cookie based session before any output to the browser. Do not forget that you may have files in include or require , which seem to contain only php-code, but in fact at the end may contain a space after the closing tag ?> . For the sake of eliminating such situations, such files are usually left without a closing tag.

    • "Session can not be stored in a cookie" - how is this done? I know only one option - this is through cookies - Dimon
    • one
      @Dimon updated the answer. In general, look here php.net/manual/ru/session.configuration.php - so extensive, where they just do not store, including self-written session handlers, installed via php.net/manual/en/session.customhandler.php - Stanislav Belichenko
    • @Dimon you are right - the first phrase in this answer is absolutely meaningless, not related to your question, nor to the sessions in general, nor to common sense as a whole. Pkhshshniki love to blurt out something such. Such an ecosystem. get used to it - Ipatiev
    • @ Ipatiev explain what you mean. - Stanislav Belichenko February