When and where should I use session_start?

后端 未结 4 1958
滥情空心
滥情空心 2020-12-11 12:47

Exactly when and where should I use session_start() in PHP?

For example, say I have a login script that sets a session variable to tell whether or not t

4条回答
  •  天涯浪人
    2020-12-11 13:18

    Unless you have output buffering enabled, the session_start() must come before anything other than headers are sent to the browser (as it sets a cookie in the header).

    It must come before you attempt to reference the $_SESSION data.

    In your example there are no html tags being output before either instance - so both would work.

    There some cost to opening a session, so if you are doing additional, non-session based validation of the request, then deferring session_start() till these checks have passed does give you a bit more resillience against DOS attacks.

提交回复
热议问题