如何防止在Wordpress中使用同一用户ID从不同的浏览器进行多次登录? [英] how can I prevent multiple login from same user id from different browsers in wordpress?

查看:152
本文介绍了如何防止在Wordpress中使用同一用户ID从不同的浏览器进行多次登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户已经从一个浏览器登录并尝试使用相同的用户ID但从另一个Web浏览器再次登录到wordpress时,如何避免这种情况? 如果用户同时从移动设备和笔记本电脑访问该网站,则每个IP地址一次登录也不能解决问题.

How to prevent this situation when a user has already logged in from one browser and tries to log in into wordpress again with the same user id but from a different web browser? One login per ip address also does not solve the problem if a user access the site from mobile and a laptop simultaneously.

推荐答案

创建一个插件或将其放在function.php下.这是 wordpress每个用户一个会话

create a plugin or place it under function.php. here is the refrence wordpress one session per user

,代码是

<?php
/*
Plugin name: Single user login
Plugin URI: 
Description:
Author: Ben May
Author URI: 
Version: 0.1
*/

if( !class_exists( 'WPSingleUserLoggin' ) )
{
    class WPSingleUserLoggin
    {
        private $session_id; 

        function __construct()
        {
            if ( ! session_id() )
                session_start();

            $this->session_id = session_id();

            add_action( 'init', array( $this, 'init' ) );
            add_action( 'wp_login', array( $this, 'wp_login' ), 10, 2 );
        }

        function init()
        {
            if( ! is_user_logged_in() )
                return;

            $stored_sess_id = get_user_meta( get_current_user_id(), '_wp_single_user_hash', true );

            if( $stored_sess_id != $this->session_id )
            {
                wp_logout(); 
                wp_redirect( wp_login_url() );
                exit;
            }
        }
        function wp_login( $user_login, $user )
        {
            update_user_meta( $user->ID, '_wp_single_user_hash', $this->session_id );
            return;
        }
    }
    new WPSingleUserLoggin();
}

更新

它将自动删除旧会话.

如果要更改功能,请在

if( $stored_sess_id != $this->session_id )
 {
   wp_logout(); 
   wp_redirect( wp_login_url() );
   exit;
 }

和此功能

function wp_login( $user_login, $user )
{
update_user_meta( $user->ID, '_wp_single_user_hash', $this->session_id );
 return;
}

这篇关于如何防止在Wordpress中使用同一用户ID从不同的浏览器进行多次登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆