奥多码,点击查看详情 97CDN云盾,点击查看详情

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

       
广告2021-06-03到期2021-07-03广告2021-06-03到期2021-07-03
       
广告2021-06-03到期2021-07-03广告2021-06-03到期2021-07-03

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

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

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

  1. <?php
  2. /*
  3. Pluginname:WPSingleLogin
  4. PluginURI:http://magnigenie.com/wp-single-login/
  5. Description:Thispluginwillautomaticallylogoutthealreadyloggedinuserwhenauserwiththesamelogindetailstriestologinfromdifferentbrowserordifferentcomputer.Thispluginneedszeroconfigurationtorun.Justinstallitifyouwantsingleloginfunctionalityonyoursite.
  6. Version:1.0
  7. Author:NirmalRam
  8. AuthorURI:http://magnigenie.com/about-me/
  9. License:GPLv2orlater
  10. LicenseURI:http://www.gnu.org/licenses/gpl-2.0.html
  11. */
  12. if(!class_exists(‘wp_single_login’)){
  13. classwp_single_login{
  14. private$session_id;
  15. function__construct(){
  16. if(!session_id())
  17. session_start();
  18. $this->session_id=session_id();
  19. add_action(‘init’,array($this,‘wpsl_init’));
  20. add_action(‘wp_login’,array($this,‘wpsl_login’),10,2);
  21. add_filter(‘heartbeat_received’,array($this,‘wpsl_heartbeat_received’),10,2);
  22. add_filter(‘heartbeat_nopriv_received’,array($this,‘wpsl_heartbeat_received’),10,2);
  23. add_filter(‘login_message’,array($this,‘wpsl_loggedout_msg’),10);
  24. }
  25. functionwpsl_init(){
  26. if(!is_user_logged_in())
  27. return;
  28. //enqueuetheHeartbeatAPI
  29. wp_enqueue_script(‘heartbeat’);
  30. wp_enqueue_script(‘jquery’);
  31. //loadourJavascriptinthefooter
  32. add_action(“wp_footer”,array($this,‘wpsl_scripts’));
  33. $user_sess_id=get_user_meta(get_current_user_id(),‘_wpsl_hash’,true);
  34. if($user_sess_id!=$this->session_id){
  35. wp_logout();
  36. wp_redirect(site_url(‘wp-login.php?wpsl=loggedout’));
  37. exit;
  38. }
  39. }
  40. functionwpsl_login($user_login,$user){
  41. update_user_meta($user->ID,‘_wpsl_hash’,$this->session_id);
  42. return;
  43. }
  44. functionwpsl_loggedout_msg(){
  45. if(isset($_GET[‘wpsl’])&&$_GET[‘wpsl’]==‘loggedout’){
  46. $msg=__(“Yoursessionhasbeenterminatedasyouareloggedinfromanotherbrowser.”);
  47. $message=‘<pclass=“message”>’.$msg.’&lt;/p><br/>’;
  48. return$message;
  49. }
  50. }
  51. functionwpsl_heartbeat_received($response,$data){
  52. $user_sess_id=get_user_meta(get_current_user_id(),‘_wpsl_hash’,true);
  53. if($data[‘user_hash’]&&amp;$data[‘user_hash’]!=$user_sess_id){
  54. $response[‘wpsl_response’]=1;
  55. wp_logout();
  56. }
  57. else
  58. $response[‘wpsl_response’]=0;
  59. return$response;
  60. }
  61. functionwpsl_scripts(){?>
  62. <script>
  63. jQuery(document).ready(function(){
  64. wp.heartbeat.interval(‘fast’);
  65. //hookintoheartbeat-send:andsendthecurrentsessionidtotheserver
  66. jQuery(document).on(‘heartbeat-send’,function(e,data){
  67. data[‘user_hash’]=‘<?phpecho$this->session_id;?>’;//needsomedatatokickoffAJAXcall
  68. });
  69. //hookintoheartbeat-tick:clientlooksfora‘server’varinthedataarrayandlogsittoconsole
  70. jQuery(document).on(‘heartbeat-tick’,function(e,data){
  71. if(data[‘wpsl_response’]){
  72. alert(‘<?php_e(‘Yoursessionhasbeenterminatedasyouareloggedinfromanotherbrowser.’);?>’);
  73. window.location.href=’https://www.xhsay.com/<?php%C2%A0echosite_url(‘wp-login.php?wpsl=loggedout’);?>‘;
  74. }
  75. });
  76. });
  77. </script>
  78. <?php
  79. }
  80. }
  81. newwp_single_login();
  82. }
本文由【好易之】整理自网络!
原创文章,作者:【好易之】如转载请注明出处:https://www.zhengjiaxi.com/zxwd/jswd/69785.html
如有侵权,请邮件联系 aoduoye@qq.com 删除。
本站发布的文章及附件仅限用于学习和研究目的;不得将上述内容用于商业或非法用途,否则后果请用户自负。
本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。
如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。
(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
好易之的头像好易之站长
上一篇 2020-05-27 00:35
下一篇 2020-05-27 00:35

相关推荐

发表回复

登录后才能评论

联系我们

400-800-8888

在线咨询:点击这里给我发消息

 

工作时间:周一至周五,9:30-18:30,节假日休息

关注公众号
请查看头部文章来源地址!本站所有内容均为互联网收集整理和网友上传。仅限于学习研究,切勿用于商业用途。否则由此引发的法律纠纷及连带责任本站概不承担。