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

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

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

  1. <?php
  2. /*
  3. Plugin name: WP Single Login
  4. Plugin URI: http://magnigenie.com/wp-single-login/
  5. 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.
  6. Version: 1.0
  7. Author: Nirmal Ram
  8. Author URI: http://magnigenie.com/about-me/
  9. License: GPLv2 or later
  10. License URI: http://www.gnu.org/licenses/gpl-2.0.html
  11. */
  12. if( !class_exists( 'wp_single_login' ) ) {
  13.   	class wp_single_login {
  14. 		private $session_id;
  15. 
    
  16. 		function __construct() {
  17. 			if ( ! session_id() )
  18. 			    session_start();
  19. 
    
  20. 			$this->session_id = session_id();
  21. 
    
  22. 			add_action( 'init', array( $this, 'wpsl_init' ) );
  23. 			add_action( 'wp_login', array( $this, 'wpsl_login' ), 10, 2 );
  24.       add_filter('heartbeat_received', array( $this, 'wpsl_heartbeat_received' ), 10, 2);
  25. 			add_filter('heartbeat_nopriv_received', array( $this, 'wpsl_heartbeat_received' ), 10, 2);
  26. 			add_filter( 'login_message', array( $this, 'wpsl_loggedout_msg' ), 10 );
  27. 		}
  28. 
    
  29. 		function wpsl_init() {
  30. 			if( ! is_user_logged_in() )
  31. 				return;
  32.       //enqueue the Heartbeat API
  33.       wp_enqueue_script('heartbeat');
  34.       wp_enqueue_script('jquery');
  35. 
    
  36.       //load our Javascript in the footer
  37.       add_action("wp_footer", array( $this, 'wpsl_scripts' ) );
  38. 			$user_sess_id = get_user_meta( get_current_user_id(), '_wpsl_hash', true );
  39. 
    
  40. 			if( $user_sess_id != $this->session_id ) {
  41. 				wp_logout();
  42. 				wp_redirect( site_url( 'wp-login.php?wpsl=loggedout' ) );
  43. 				exit;
  44. 			}
  45. 		}
  46. 		function wpsl_login( $user_login, $user ) {
  47. 			update_user_meta( $user->ID, '_wpsl_hash', $this->session_id );
  48. 			return;
  49. 		}
  50. 		function wpsl_loggedout_msg() {
  51. 				if ( isset($_GET
    ['wpsl']) && $_GET['wpsl'] == 'loggedout' ) {
  52. 						$msg = __( "Your session has been terminated as you are logged in from another browser." ) ;
  53. 						$message = '<p >'.$msg.'</p><br />';
  54. 						return $message;
  55. 				}
  56. 		}
  57. function wpsl_heartbeat_received($response, $data) {
  58.   $user_sess_id = get_user_meta( get_current_user_id(), '_wpsl_hash', true );
  59. 	if( $data['user_hash'] && $data['user_hash'] != $user_sess_id ){
  60. 		$response['wpsl_response'] = 1;
  61.     wp_logout();
  62. 	}
  63.   else
  64.     $response['wpsl_response'] = 0;
  65. 
    
  66. 	return $response;
  67. }
  68. 
    
  69. function wpsl_scripts() { ?>
  70. <script>
  71.   jQuery(document).ready(function() {
  72. 		wp.heartbeat.interval( 'fast' );
  73. 		//hook into heartbeat-send: and send the current session id to the server
  74. 		jQuery(document).on('heartbeat-send', function(e, data) {
  75. 			data['user_hash'] = '<?php echo $this->session_id; ?>';	//need some data to kick off AJAX call
  76. 		});
  77. 
    
  78. 		//hook into heartbeat-tick: client looks for a 'server' var in the data array and logs it to console
  79. 		jQuery(document).on( 'heartbeat-tick', function( e, data ) {
  80. 			if( data['wpsl_response'] ){
  81.         alert( '<?php _e('Your session has been terminated as you are logged in from another browser.'); ?>' );
  82. 				window.location.href='<?php echo site_url( 'wp-login.php?wpsl=loggedout' ); ?> ';
  83. 			}
  84. 		});
  85. 	});
  86. </script>
  87. <?php
  88. }
  89. 	}
  90. 	new wp_single_login();
  91. }