简体   繁体   中英

Does session_start in PHP prevent session expiration?

I'm somewhat bewildered about using session_start in PHP . Should I use it in my scripts both when user initially creates session and when resumes it on consequent queries - or only when creating?

Currently I do not call it when session already exists but I have found that session sometimes expires unexpectedly fast:

For example, from public log of my site I see:

srinivasvarma678 09:14:34 27-Jun-14
    I've just logged in...

...

srinivasvarma678 08:59:38 27-Jun-14
    I'm proud to tell I've just solved Vowel Count!

Ie user's last interaction with site was at 8:59 and then in 15 minutes he needs to log in again (though session.gc_maxlifetime=1440 )

Could this behavior be explained by the fact I am not calling session_start every time?

Short answer: Yes

As stated here in 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.

So if you want to continue the session, you should always use session_start() on every page...

session_start() allows you to use sessions in your script.

You should use it both when creating new sessions or reusing existent one.

It also updates the session, to it won't be garbage collected and removed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM