简体   繁体   中英

how can I prevent multiple login from same user id from different browsers in wordpress?

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.

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

and the code is

<?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();
}

UPDATE

it will autometically destroy the old session .

if you want to change the functionality do your stuff in

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

and this function

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM