Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Session::close() #7508

Merged
merged 1 commit into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions system/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,20 @@ public function destroy()
session_destroy();
}

/**
* Writes session data and close the current session.
*
* @return void
*/
public function close()
{
if (ENVIRONMENT === 'testing') {
return;
}

session_write_close();
}

/**
* Sets user data into the session.
*
Expand Down
26 changes: 25 additions & 1 deletion user_guide_src/source/libraries/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,30 @@ intend to reuse that same key in the same request, you'd want to use

.. literalinclude:: sessions/036.php

Closing a Session
=================

.. _session-close:

close()
-------

.. versionadded:: 4.4.0

To close the current session manually after you no longer need it, use the
``close()`` method:

.. literalinclude:: sessions/044.php

You do not have to close the session manually, PHP will close it automatically
after your script terminated. But as session data is locked to prevent concurrent
writes only one request may operate on a session at any time. You may improve
your site performance by closing the session as soon as all changes to session
data are done.

This method will work in exactly the same way as PHP's
`session_write_close() <https://www.php.net/session_write_close>`_ function.

Destroying a Session
====================

Expand Down Expand Up @@ -529,7 +553,7 @@ DatabaseHandler Driver
supported, due to lack of advisory locking mechanisms on other
platforms. Using sessions without locks can cause all sorts of
problems, especially with heavy usage of AJAX, and we will not
support such cases. Use ``session_write_close()`` after you've
support such cases. Use the :ref:`session-close` method after you've
done processing session data if you're having performance
issues.

Expand Down
2 changes: 1 addition & 1 deletion user_guide_src/source/libraries/sessions/003.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

session_write_close();
$session->close();
3 changes: 3 additions & 0 deletions user_guide_src/source/libraries/sessions/044.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

$session->close();