关于 WordPress 禁止多个人同时登录一个用户账号,今天推荐的 Wp Single Login 可以实现这样的功能,不过 Wp Single Login 是通过 WP 3.6 新增的 Heartbeat API? 来实现的。
后台插件安装界面搜索 Wp Single Login 即可在线安装,或者在这里下载 Wp Single Login ,直接安装启用即可,不需要设置。
当然,如果你不想用插件,或者想自定义某些代码,以下就是该插件的源代码:
- <?php
- /*
- Pluginname:WPSingleLogin
- PluginURI:http://magnigenie.com/wp-single-login/
- Description:Thispluginwillautomaticallylogoutthealreadyloggedinuserwhenauserwiththesamelogindetailstriestologinfromdifferentbrowserordifferentcomputer.Thispluginneedszeroconfigurationtorun.Justinstallitifyouwantsingleloginfunctionalityonyoursite.
- Version:1.0
- Author:NirmalRam
- AuthorURI:http://magnigenie.com/about-me/
- License:GPLv2orlater
- LicenseURI:http://www.gnu.org/licenses/gpl-2.0.html
- */
- if(!class_exists(‘wp_single_login’)){
- classwp_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);
- }
- functionwpsl_init(){
- if(!is_user_logged_in())
- return;
- //enqueuetheHeartbeatAPI
- wp_enqueue_script(‘heartbeat’);
- wp_enqueue_script(‘jquery’);
- //loadourJavascriptinthefooter
- 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;
- }
- }
- functionwpsl_login($user_login,$user){
- update_user_meta($user->ID,‘_wpsl_hash’,$this->session_id);
- return;
- }
- functionwpsl_loggedout_msg(){
- if(isset($_GET[‘wpsl’])&&$_GET[‘wpsl’]==‘loggedout’){
- $msg=__(“Yoursessionhasbeenterminatedasyouareloggedinfromanotherbrowser.”);
- $message=‘<pclass=“message”>’.$msg.’</p><br/>’;
- return$message;
- }
- }
- functionwpsl_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;
- }
- functionwpsl_scripts(){?>
- <script>
- jQuery(document).ready(function(){
- wp.heartbeat.interval(‘fast’);
- //hookintoheartbeat-send:andsendthecurrentsessionidtotheserver
- jQuery(document).on(‘heartbeat-send’,function(e,data){
- data[‘user_hash’]=‘<?phpecho$this->session_id;?>’;//needsomedatatokickoffAJAXcall
- });
- //hookintoheartbeat-tick:clientlooksfora‘server’varinthedataarrayandlogsittoconsole
- jQuery(document).on(‘heartbeat-tick’,function(e,data){
- if(data[‘wpsl_response’]){
- alert(‘<?php_e(‘Yoursessionhasbeenterminatedasyouareloggedinfromanotherbrowser.’);?>’);
- window.location.href=’https://www.xhsay.com/<?php%C2%A0echosite_url(‘wp-login.php?wpsl=loggedout’);?>‘;
- }
- });
- });
- </script>
- <?php
- }
- }
- newwp_single_login();
- }