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

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

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

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

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

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

Prevent Concurrent Logins提取:

  1. function pcl_user_has_concurrent_sessions() {
  2.     return ( is_user_logged_in() && count( wp_get_all_sessions() ) > 1 );
  3. }
  4. /**
  5.  * Get the user's current session array
  6.  *
  7.  * @return array
  8.  */
  9. function pcl_get_current_session() {
  10.     $sessions = WP_Session_Tokens::get_instance( get_current_user_id() );
  11.     return $sessions->get( wp_get_session_token() );
  12. }
  13. /**
  14.  * Only allow one session per user
  15.  *
  16.  * If the current user's session has been taken over by a newer
  17.  * session then we will destroy their session automattically and
  18.  * they will have to login again to continue.
  19.  *
  20.  * @action init
  21.  *
  22.  * @return void
  23.  */
  24. function pcl_disallow_account_sharing() {
  25.     if ( ! pcl_user_has_concurrent_sessions() ) {
  26.         return;
  27.     }
  28.     $newest  = max( wp_list_pluck( wp_get_all_sessions(), 'login' ) );
  29.     $session = pcl_get_current_session();
  30.     if ( $session['login'] === $newest ) {
  31.         wp_destroy_other_sessions();
  32.     } else {
  33.         wp_destroy_current_session();
  34.     }
  35. }
  36. add_action( 'init', 'pcl_disallow_account_sharing' );

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 class="message">'.$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. }  

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如果您也有好的资源或教程,您可以投稿发布,成功分享后有M币奖励和额外收入!

模板下载吧 Wordpress教程 WordPress 禁止多人同时登录一个用户账号(代码版) https://www.mbxzb.com/blog/wordpress/10719.html

常见问题
  • 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承
查看详情
  • 最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用
查看详情

相关文章

评论
暂无评论
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 威武 友军 顶贴
官方客服团队

为您解决烦忧 - 24小时在线 专业服务