Contributors: getpantheon, [outlandish josh](https://profiles.wordpress.org/outlandish josh), mpvanwinkle77, danielbachhuber, andrew.taylor
Tags: comments, sessions
Requires at least: 4.7
Tested up to: 5.8
Stable tag: 1.2.4
Requires PHP: 5.4
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Use native PHP sessions and stay horizontally scalable. Better living through superior technology.
WordPress core does not use PHP sessions, but sometimes they are required by your use-case, a plugin or theme.
This plugin implements PHP's native session handlers, backed by the WordPress database. This allows plugins, themes, and custom code to safely use PHP $_SESSION
s in a distributed environment where PHP's default tempfile storage just won't work.
Note that primary development is on GitHub if you would like to contribute:
https://github.com/pantheon-systems/wp-native-php-sessions
- Upload to the
/wp-content/plugins/
directory - Activate the plugin through the 'Plugins' menu in WordPress
That's it!
The best way to contribute to the development of this plugin is by participating on the GitHub project:
https://github.com/pantheon-systems/wp-native-php-sessions
Pull requests and issues are welcome!
You may notice there are two sets of tests running, on two different services:
- Travis CI runs the PHPUnit test suite.
- Circle CI runs the Behat test suite against a Pantheon site, to ensure the plugin's compatibility with the Pantheon platform.
Both of these test suites can be run locally, with a varying amount of setup.
PHPUnit requires the WordPress PHPUnit test suite, and access to a database with name wordpress_test
. If you haven't already configured the test suite locally, you can run bash bin/install-wp-tests.sh wordpress_test root '' localhost
.
Behat requires a Pantheon site. Once you've created the site, you'll need install Terminus, and set the TERMINUS_TOKEN
, TERMINUS_SITE
, and TERMINUS_ENV
environment variables. Then, you can run ./bin/behat-prepare.sh
to prepare the site for the test suite.
This implements the built-in PHP session handling functions, rather than introducing anything custom. That way you can use built-in language functions like the $_SESSION
superglobal and session_start()
in your code. Everything else will "just work".
PHP's fallback default functionality is to allow sessions to be stored in a temporary file. This is what most code that invokes sessions uses by default, and in simple use-cases it works, which is why so many plugins do it.
However, if you intend to scale your application, local tempfiles are a dangerous choice. They are not shared between different instances of the application, producing erratic behavior that can be impossible to debug. By storing them in the database the state of the sessions is shared across all application instances.
If you see an error like "Fatal error: session_start(): Failed to initialize storage module:" or "Warning: ini_set(): A session is active.", then you likely have a plugin that is starting a session before WP Native PHP Sessions is loading.
To fix, create a new file at wp-content/mu-plugins/000-loader.php
and include the following:
<?php
require_once WP_PLUGIN_DIR . '/wp-native-php-sessions/pantheon-sessions.php';
This mu-plugin will load WP Native PHP Sessions before all other plugins, while letting you still use the WordPress plugin updater to keep the plugin up-to-date.
- Increases data blob size from 64k to 16M for new session tables; existing tables will need to manually modify the column if they want to apply this change [#193].
- Assigns the table name to a variable before using in query [#188].
- Includes an auto-incrementing
id
column for replication support [#187].
- Plugin textdomain needs to be the same as the WordPress.org slug [#169].
- Avoids using cookies for sessions when WP-CLI is executing [#154].
- Avoids initializing PHP sessions when doing cron [#149].
- Plugin is stable.
- Refactors session callback logic into
Session_Handler
abstraction, fixing PHP notice in PHP 7.3 [#135].
- Fixes handling of 'X-Forwarded-For' header in
get_client_ip_server()
[#126].
- Respects various
HTTP_*
sources for client IP address [#122].
- Adds a safety check that restores
$wpdb
when it's missing.
- Ensures
_pantheon_session_destroy()
uses a return value.
- Switches to
E_USER_WARNING
instead ofE_WARNING
when triggering errors.
- Disables plugin load when
WP_INSTALLING
, because session table creation breaks installation process.
- Restores session instantiation when WP-CLI is executing, because not doing so causes other problems.
- Disables session instantiation when
defined( 'WP_CLI' ) && WP_CLI
because sessions don't work on CLI.
- Triggers PHP error when plugin fails to write session to database.
- Returns false when we entirely fail to generate a session.
- Syncs session user id when a user logs in and logs out.
- Bug fix: Prevents warning session_write_close() expects exactly 0 parameters, 1 given.
- Bug fix: Prevents PHP fatal error in
session_write_close()
by running on WordPress'shutdown
action, before$wpdb
destructs itself. - Bug fix: Stores the actual user id in the sessions table, instead of
(bool) $user_id
.
- Compatibility with PHP 7.
- Adds
pantheon_session_expiration
filter to modify session expiration value.
- Adjustment to
session_id()
behavior for wider compatibility - Using superglobal for REQUEST_TIME as opposed to
time()
- Fixes issue related to WordPress plugin load order
- Initial release