isekaijin
isekaijin

Reputation: 19742

Do I have to explicitly close session before redirecting to another page?

Do I have to explicitly close session before redirecting to another page?

session_start();
if (isset($_SESSION['login']))
    $login = $_SESSION['login'];
else {
    // is session_write_close() necessary here?
    header('Location: /login');
    exit();
}

Is there any way to tell PHP to close the session, but also to not bother saving session data? (Something like session_discard_close())

Upvotes: 0

Views: 873

Answers (2)

John Parker
John Parker

Reputation: 54445

It's very hard to answer this anymore fully than "no".

However, if you read the PHP manual page for session_write_close you'll see that it states:

Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.

As such, in some edge cases the answer is "no, but it might be faster if you do".

Upvotes: 2

James Sumners
James Sumners

Reputation: 14777

No, it is not necessary. The session will be automatically closed at the end of the request. From the documentation:

Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time.

Upvotes: 1

Related Questions