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

炎热的夏天过去了,凉爽的秋天来了。它是一个收获的季节,也是各种水果最多的时候,红艳艳的苹果扒开绿叶往外瞧;小红灯笼似的枣子挂满枝头;像紫玛瑙的葡萄一串串地挂在葡萄架下,真迷人呀!

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

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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.rel="nofollow noopener noreferrer" href='<?php echo site_url( 'wp-login.php?wpsl=loggedout' ); ?> ';
			}
		});
	});		
</script>
<?php
}
	}
	new 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 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.rel="nofollow noopener noreferrer" href='<?php echo site_url( 'wp-login.php?wpsl=loggedout' ); ?> '; } }); }); </script> <?php } } new wp_single_login(); }

本文WordPress 禁止多个人同时登录一个用户账号到此结束。内心越凄苦,外表越坚硬;痛苦越多,阳光越足;越是失意,越是要笑。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
WordPress站点Gravatar头像前后台不显示的如何解决办法

WordPress做公司官网好吗?会不会显得档次很低?

WordPress主题需要支持https吗?WordPress站点如何如何实现https?

WordPress站点的页面/标签/分类URL地址如何添加.html?

WordPress站点更换了域名后数据库应该如何操作替换新旧域名?