开发者

session_id() always returns '' even if a session cookie is set

开发者 https://www.devze.com 2023-04-02 05:18 出处:网络
I am currently experiencing problems with the PHP function session_id. At the beginning of my scripts, I want to check whet开发者_开发百科her the user has a session and I do not want to call session_

I am currently experiencing problems with the PHP function session_id.

At the beginning of my scripts, I want to check whet开发者_开发百科her the user has a session and I do not want to call session_start() as that would generate a session cookie.

Whenever I call session_id, it returns '' , even if a session is definetely set. I verified that a session is set by checking for the session cookie via the $_COOKIE array.


session_start() will not generate a new session. From the PHP docs:

session_start() creates a session or resumes the current one based on a session identifier passed via a GET or POST request, or passed via a cookie.

You have to resume the old session before you can get its ID.


Without calling session_start() the session does not exist for PHP. After session_start() you can easily check whether the user has got a session with checking for a key in the $_SESSION super-global array.


If you want to check if they have a session and get the session id without creating a new one if they don't your best bet is to look in the $_COOKIE array for the session id (or $_GET if you use that for storing the id instead).

session_id() will only work if you call session_start(). If the session does already exist it will not generate a new session id so it will still give you the session id you are looking for.

0

精彩评论

暂无评论...
验证码 换一张
取 消