1

Hope someone can help me. My client and I are down to the wire on this project and I can't seem to figure out a solution for this problem.

I'm currently using WP Bouncer to limit the amount of devices logged in to a user account to one device. I have the Groups plugin setup to restrict certain pages on the site as well.

The problem: The WP Bouncer plugin logs a user off on one device once another device logs in. However the user must go to another page in order to see that they were logged out. This creates a problem for my client. Once a user is on a restricted page on one device, he can login on another device and view the same restricted page, allowing both devices to view the page at once. Is there a way to check periodically if a user is logged in? I don't want to use any idle user plugins because the restricted pages include videos and digital magazines. Any advice would be appreciated!

closed as off-topic by Mark Kaplun, TheDeadMedic, Howdy_McGee, Pieter Goosen, Sisir Oct 17 '15 at 11:31

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    I think that the main problem of your client is not having a clue how the web works. Does he know about print screen and general print functions? or about proxies? Anyway this is either too broad or just off-topic here. – Mark Kaplun Sep 30 '15 at 19:20
  • Agreed. Sometimes clients need feedback too. – TheDeadMedic Oct 1 '15 at 9:29
0

I would use AJAX to check if the user is logged in. You could use setInterval to check every 30 seconds (or whatever).

I tested the following code, and it works. First, create a file in your theme directory called ajax.js and put this code in it:

(function ( $ ) {
    var check_user_is_logged_in = function(){
        $.ajax({
            type: 'POST',
            url: ajax_object.ajax_url,
            data: {action:'is_user_logged_in'},
            success:function(data) {
                if ( data == 'no' ) {
                    window.location.href = "http://mywebsite.com";
                }
            },
            error: function(errorThrown){
                console.log(errorThrown);
            }
        });
    };

    setInterval(function(){
        check_user_is_logged_in();
    }, 30000);
})( jQuery );

In the above code, change "mywebsite.com" to whatever URL you want to redirect logged out users to. You can also adjust 30000. That's the number of milliseconds it should wait between checking.

Next, in your theme's functions.php file, add this code:

function na_check_user_logged_in() {
    // Simply check if the user is logged in or not and echo 'yes' or 'no'
    echo ( is_user_logged_in() ) ? 'yes' : 'no';
    wp_die();
}

function na_enqueue_scripts(){
    // Enqueue our ajax.js script
    wp_enqueue_script( 'na_ajax_example', get_stylesheet_directory_uri().'/ajax.js', 'jquery');

    // Reveal the ajax_url for our script     
    wp_localize_script( 'na_ajax_example', 'ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
};

add_action( 'wp_ajax_is_user_logged_in', 'na_check_user_logged_in' );
add_action( 'wp_ajax_nopriv_is_user_logged_in', 'na_check_user_logged_in' );
add_action( 'wp_enqueue_scripts', 'na_enqueue_scripts' );

Right now, this code will run on every page. You will need to edit the na_enqueue_scripts function and add some logic so it only enqueues the script on the protected pages.

  • You're very welcome! – Nate Allen Oct 1 '15 at 18:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.