当前位置:首页>WordPress建站>用户交互>WordPress 禁止多个人同时登录一个用户账号

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

WordPress 主题 CoreNext

关于 WordPress 禁止多个人同时登录一个用户账号,倡萌之前就推荐过 Prevent Concurrent Logins ,今天推荐的 Wp Single Login 也可以实现一样的功能,不过 Wp Single Login 是通过 WP 3.6 新增的 Heartbeat API  来实现的。

后台插件安装界面搜索 Wp Single Login 即可在线安装,或者在这里下载 Wp Single Login ,直接安装启用即可,不需要设置。

2020年6月测试:已测试该插件兼容至 WordPress 5.4.2,后续版本是否兼容,大家自己测试。

当然,如果你不想用插件,或者想自定义某些代码,以下就是该插件的源代码:

<?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
			wp_enqueue_script('heartbeat');
			wp_enqueue_script('jquery');
      
			//load our Javascript in the footer
			add_action("wp_footer", array( $this, 'wpsl_scripts' ) );

			$user_sess_id = get_user_meta( get_current_user_id(), '_wpsl_hash', true );
			
			if( $user_sess_id != $this->session_id ) {
				wp_logout(); 
				wp_redirect( site_url( 'wp-login.php?wpsl=loggedout' ) );
				exit;
			}
		}

		function wpsl_login( $user_login, $user ) {
			update_user_meta( $user->ID, '_wpsl_hash', $this->session_id );
			return;
		}

		function wpsl_loggedout_msg() {
				if ( isset($_GET['wpsl']) && $_GET['wpsl'] == 'loggedout' ) {
						$msg = __( "Your session has been terminated as you are logged in from another browser." ) ;
						$message = '<p class="message">'.$msg.'</p><br />';
						return $message;
				}
		}

		function wpsl_heartbeat_received($response, $data) {
			$user_sess_id = get_user_meta( get_current_user_id(), '_wpsl_hash', true );
			if( $data['user_hash'] && $data['user_hash'] != $user_sess_id ){
				$response['wpsl_response'] = 1;
				wp_logout();
			}else{
				$response['wpsl_response'] = 0;
			}
			return $response;
		}
    
		function wpsl_scripts() { ?>
			<script>
			jQuery(document).ready(function() {
				wp.heartbeat.interval( 'fast' );
				//hook into heartbeat-send: and send the current session id to the server
				jQuery(document).on('heartbeat-send', function(e, data) {
					data['user_hash'] = '<?php echo $this->session_id; ?>';	//need some data to kick off AJAX call
				});
				
				//hook into heartbeat-tick: client looks for a 'server' var in the data array and logs it to console
				jQuery(document).on( 'heartbeat-tick', function( e, data ) {			
					if( data['wpsl_response'] ){
						alert( '<?php _e('Your session has been terminated as you are logged in from another browser.'); ?>' );
						window.location.href='<?php echo site_url( 'wp-login.php?wpsl=loggedout' ); ?> ';
					}
				});
			});		
			</script>
		<?php
		}
	}
	new wp_single_login();
}
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
欢迎关注WordPress大学公众号 WPDAXUE
用户交互

WordPress 使用 Simple User Signups 快速注册账号

2015-11-24 11:28:07

用户交互

WordPress 使用 Apply Online 添加工作/广告/公开课等网上报名表单

2016-3-13 11:13:22

4 条回复 A文章作者 M管理员
  1. zz999

    请问下站长,现在post2qzone插件都失效了,有什么插件可以自动同步到QQ空间或者说说的吗

  2. xiao伟

    添加模版函数文件里?

  3. xihaduanzi

    学习了,不错! 必须收藏

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索