WordPress 禁止多人同时登录一个用户账号(代码版)

作者 : 轩寒 本文共2165个字,预计阅读时间需要6分钟 发布时间: 2022-05-1 共131人阅读

在WordPress的用户管理问题中发现WordPress有一个很头疼的问题,那就是,WordPress居然可以多个人同事登陆一个账号,不知道目前的4.8版本是否还允许,这里也不在测试,直接分享一段代码,实现:WordPress 禁止多人同时登录一个用户账号。

WordPress 禁止多人同时登录一个用户账号

这里推荐的功能插件为:Prevent Concurrent Logins和Wp Single Login点击即可直接下载。

如果你不想用插件,这里是代码版,添加到function.php即可:

以下分别为两个插件提取的代码版!

Prevent Concurrent Logins提取:

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' );

Wp Single Login提取:

<?php
/*
Plugin name: WP Single Login
Plugin URI: http://magnigenie.com/wp-single-login/
Description: This plugin will automatically logout the already logged in user when a user with the same login details tries to login from different browser or different computer. This plugin needs zero configuration to run. Just install it if you want single login functionality on your site.
Version: 1.0
Author: Nirmal Ram
Author URI: http://magnigenie.com/about-me/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if( !class_exists( 'wp_single_login' ) ) {
  	class wp_single_login {
		private $session_id; 
 
		function __construct() {
			if ( ! session_id() )
			    session_start();
 
			$this->session_id = session_id();
 
			add_action( 'init', array( $this, 'wpsl_init' ) );
			add_action( 'wp_login', array( $this, 'wpsl_login' ), 10, 2 );
      add_filter('heartbeat_received', array( $this, 'wpsl_heartbeat_received' ), 10, 2);
			add_filter('heartbeat_nopriv_received', array( $this, 'wpsl_heartbeat_received' ), 10, 2);
			add_filter( 'login_message', array( $this, 'wpsl_loggedout_msg' ), 10 );
		}
 
		function wpsl_init() {
			if( ! is_user_logged_in() )
				return;
      //enqueue the Heartbeat API
版权免责声明

① 本站VIP源码资源永久免费下载 www.wayu.cn;
② 本网站资源来源于网络收集,如有侵权,请联系站长进行删除处理;
③ 分享目的仅供大家学习和交流,请不要用于商业用途,否则后果自负;
④ 如果你有源码需要出售,可以联系小编,或者点击 投稿;
⑤ 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解;
⑥ 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需;
⑦ 在您的能力范围内,为了大环境的良性发展,请尽可能的选择正版资源;
⑧ 网站资源不做任何二次加密和授权后门(原版加密除外)。


站哈源码网 » WordPress 禁止多人同时登录一个用户账号(代码版)

常见问题FAQ

免费下载或者VIP会员专享资源能否直接商用?
本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。

发表评论

Copyright© 2022 by-站哈源码网 & 站内部分资源收集于网络,若侵犯了您的合法权益,请联系站长删除!
在 1.807 秒钟有 39 次查询。
开通VIP 享更多特权,建议使用QQ登录