wp_destroy_other_sessions()
云策文档标注
概述
wp_destroy_other_sessions() 函数用于从数据库中移除当前用户除当前会话令牌外的所有其他会话令牌,以增强安全性。
关键要点
- 函数移除当前用户的所有其他会话,仅保留当前会话令牌。
- 通过 WP_Session_Tokens 类实例调用 destroy_others() 方法实现。
- 需要先获取当前会话令牌和用户 ID。
代码示例
function wp_destroy_other_sessions() {
$token = wp_get_session_token();
if ( $token ) {
$manager = WP_Session_Tokens::get_instance( get_current_user_id() );
$manager->destroy_others( $token );
}
}注意事项
- 函数自 WordPress 4.0.0 版本引入。
- 依赖 wp_get_session_token() 和 get_current_user_id() 函数。
- 适用于需要强制用户登出其他设备或会话的场景。
原文内容
Removes all but the current session token for the current user for the database.
Source
function wp_destroy_other_sessions() {
$token = wp_get_session_token();
if ( $token ) {
$manager = WP_Session_Tokens::get_instance( get_current_user_id() );
$manager->destroy_others( $token );
}
}
Changelog
| Version | Description |
|---|---|
| 4.0.0 | Introduced. |