對于開放注冊的 WordPress 站點(diǎn)來說,尤其是有會(huì)員購買服務(wù)的站點(diǎn),可能需要禁止用戶共享賬號,也就是要禁止多個(gè)人同時(shí)登錄一個(gè)賬號。倡萌今天分享老外的一個(gè)方法,大家不妨試試。
將下面的代碼到主題的 functions.php 中即可:
/**
* Detect if the current user has concurrent sessions
*
* @return bool
*/
function pcl_user_has_concurrent_sessions() {
return ( is_user_logged_in() && count( wp_get_all_sessions() ) > 1 );
}
/**
* Get the user's current session array
*
* @return array
*/
function pcl_get_current_session() {
$sessions = WP_Session_Tokens::get_instance( get_current_user_id() );
return $sessions->get( wp_get_session_token() );
}
/**
* Only allow one session per user
*
* If the current user's session has been taken over by a newer
* session then we will destroy their session automattically and
* they will have to login again to continue.
*
* @action init
*
* @return void
*/
function pcl_disallow_account_sharing() {
if ( ! pcl_user_has_concurrent_sessions() ) {
return;
}
$newest = max( wp_list_pluck( wp_get_all_sessions(), 'login' ) );
$session = pcl_get_current_session();
if ( $session['login'] === $newest ) {
wp_destroy_other_sessions();
} else {
wp_destroy_current_session();
}
}
add_action( 'init', 'pcl_disallow_account_sharing' );
如果大家不想折騰代碼,可以直接下載插件 Prevent Concurrent Logins 安裝啟用也是一樣的。
2020年6月更新:該代碼和插件不兼容新版本W(wǎng)P了,請使用 Wp Single Login 替代。
聲明:本站所有文章,如無特殊說明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。





用了,不錯(cuò)。
從安全考慮,這個(gè)還是比較有用的
撒花!