How can you check if a PHP session exists?

前端 未结 11 1325
孤独总比滥情好
孤独总比滥情好 2021-02-04 06:11

Just wondering how to check if a PHP session exists... My understanding is that no matter what, if I am using sessions, I have to start my files with session_start() to even acc

11条回答
  •  死守一世寂寞
    2021-02-04 07:08

    In PHP there is something called the session name. The name is co-related to the cookie that will be being set if the session was already started.

    So you can check the $_COOKIE array if there is a session cookie available. Cookies are normally the preferred form to interchange the session id for the session name with the browser.

    If a cookie already exists this means that a PHP session was started earlier. If not, then session_start() will create a new session id and session.

    A second way to check for that is to check the outgoing headers if the cookie for the session is set there. It will be set if it's a new session. Or if the session id changed.

提交回复
热议问题