Better single session login

Home Forums Chit Chat WPLMS customizations Better single session login

Viewing 1 post (of 1 total)
  • Author
    Posts
  • #187606
    a23
    Participant
    If option is selected, by default WPLMS dows not allow to user to login unless they have logged out from other computer / session & gives a error message. But users rarely remember to logout their sessions. Instead posting this code that will automatically logout the users previous session & login to current computer. This is achieved by storing the users latest session inside user meta fields & then comparing the current session with it. PS You will logged out of admin on first time activation
    
    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();
    };
    
    
    Original code link : https://benmay.org/wordpress/single-user-wordpress/
Viewing 1 post (of 1 total)
  • The topic ‘Better single session login’ is closed to new replies.