
內容簡介
WordPress 核心不使用 PHP sessions,但有時您的使用案例、外掛或佈景主題可能需要使用它們。
此外掛實現了 PHP 的原生 session handlers,後台由 WordPress 資料庫支援。這可讓外掛、佈景主題和自訂程式碼在 PHP 預設暫存檔無法運作的分散式環境中安全地使用 PHP $_SESSION。
請注意,如果您想要貢獻,主要開發在 GitHub 上進行:
https://github.com/pantheon-systems/wp-native-php-sessions
配置
預設會話壽命為 0,即到瀏覽器關閉為止。
要覆蓋此設定,請在 WordPress Native PHP Sessions 外掛載入之前使用 pantheon_session_expiration 過濾器。例如,一個小型的 Must-use 外掛(也稱為 mu-外掛)可能包含以下內容:
<?php
function my_session_expiration_override() {
return 60*60*4; // 4小時
}
add_filter( 'pantheon_session_expiration', 'my_session_expiration_override' );<h3>貢獻</h3>
有關貢獻信息,請參閱CONTRIBUTING.md。
疑難排解
如果出現類似「致命錯誤:session_start():初始化存儲模塊失敗:」或「警告:ini_set():活動的 session。」的錯誤,那麼您可能有一個在 WP Native PHP Sessions 載入之前啟動會話的外掛。
若要修復此問題,在 wp-content/mu-plugins/000-loader.php 創建一個新檔案並包含以下內容:
<?php
require_once WP_PLUGIN_DIR . '/wp-native-php-sessions/pantheon-sessions.php';
這個 mu-外掛將在所有其他外掛之前加載 WP Native PHP Sessions,同時讓您仍然可以使用 WordPress 外掛更新程式來維護外掛的最新狀態。
外掛標籤
開發者團隊
📦 歷史版本下載
原文外掛簡介
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 $_SESSIONs 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
Configuration
By default the session lifetime is set to 0, which is until the browser is closed.
To override this use the pantheon_session_expiration filter before the Native PHP Sessions plugin is loaded. For example a small Must-use plugin (a.k.a. mu-plugin) could contain:
CLI Commands
wp pantheon session add-index
Added in 1.4.0. This command should be run if your installation of the plugin occurred before the addition of the primary ID key to the session table in version 1.2.2. You will be automatically notified when you visit any admin page if this is the case. If there’s no message, your version is good to go. Note that this command is non-destructive, a new table will be created and the existing one preserved in a backup state until you have verified that the upgrade is functioning as expected.
wp pantheon session primary-key-finalize
Added in 1.4.0. If you have run the add-index command and have verified that the new table is functioning correctly, running the primary-key-finalize command will perform a database cleanup and remove the backup table.
wp pantheon session primary-key-revert
Added in 1.4.0. If you have run the add-index command and something unexpected has occurred, just run the primary-key-revert command and the backup table will immediately be returned to being the active table.
WordPress Multisite
As of 1.4.2 the add-index, primary-key-add and primary-key-revert commands are fully multisite compatible.
Contributing
See CONTRIBUTING.md for information on contributing.
Troubleshooting
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:
