skip to Main Content

I have a site where i sell streaming videos, and i’m currently using a plugin that prevents account sharing by allowing only one device logged at a time. The problem is, the user can start watching the stream and then share his account, the second person will access the stream and both of them will watch because User 1 didn’t refresh the page (which will log him out).
How can i make the site automatically check every x minutes if user 1 is still logged in and if he isn’t, refresh the page?

2

Answers


  1. One way to do this would be to have the page call out to you on an interval using Javascript asynchronously. (e.g. ajax)

    Depending on how the streaming works you could even terminate further streaming if you do not get the interval call after a set amount of time.

    Login or Signup to reply.
  2. First of all, this is a great question !

    The problem here is to periodically check if a user is logged in, tho as you understand PHP doesn’t refresh on its own.

    Like @Chris Haas pointed out, the following article "Check if user is logged in using JQuery" is pretty close to the behavior you’re looking for, tho we need to set up, and understand a few thing.

    We can pass variable from PHP to Javascript through the wp_localize_script() WordPress function…

    …Tho, if we just localize the is_user_logged_in() as a variable, then the function would only be trigger once, meaning when the script is actually enqueued.

    The answer to our problem is triggering back a PHP function from our Javascript through Ajax.

    From our function.php: Since Ajax is already built into the core WordPress administration screens, passing it to the front end is fairly easy.

    <?php
    
    //enqueue jquery
    wp_enqueue_script( 'jquery-min', 'https://code.jquery.com/jquery-3.6.1.min.js', array(), '3.6.1', true );
    
    //add jquery integrity key
    add_filter( 'script_loader_tag', function ( $tag, $handle, $src ) {
    
        if ( 'jquery-min' === $handle ) {
    
            $tag = str_replace(
                "></script>",
                " integrity='sha256-o88AwQnZB+VDvE9tvIXrMQaPlFFSUTR+nldQm1LuPXQ=' crossorigin='anonymous'></script>",
                $tag
            );
    
        };
    
        return $tag;
    
    }, 10, 3 );
    
    //enqueue my-script
    wp_enqueue_script( 'my-script', trailingslashit( get_template_directory_uri() ) . 'assets/js/my-script.js', array( 'jquery-min' ), wp_get_theme()->version, true );
    
    //pass ajax and a nonce to my-script
    wp_localize_script( 'my-script', 'localize', array(
        '_ajax_url' => admin_url( 'admin-ajax.php' ),
        '_ajax_nonce' => wp_create_nonce( '_ajax_nonce' ),
    ) );
    

    Keep in mind that Ajax is dependend on JQuery, that’s why my-script.js is depended on JQuery.

    From our my-script.js: Our Ajax request is wrapped in a setInterval method firing every seconds. If the answer sent from the matching PHP function is false we force reload the whole page through location.reload().

    setInterval( function ( event ) {
        $.ajax( {
            type: 'POST',
            url: localize._ajax_url,
            context: this,
            data: {
                _ajax_nonce: localize._ajax_nonce,
                action: '_wpso_73857697_is_user_logged_in',
            },
            success: function ( response ) {
                let is_user_logged_in = response.data;
                switch ( is_user_logged_in ) {
                    case true:
                        break;
                    case false:
                        location.reload();
                        break;
                    default:
                        break;
                };
            },
        } );
    }, 1000 );
    

    From our function.php: The matching PHP function will check if the user is logged-in upon receiving the request sent by our self triggering Ajax function.
    It will send back a response containing the user status (true/false).

    <?php
    
    //for NON-logged-in users
    add_action( 'wp_ajax_nopriv__wpso_73857697_is_user_logged_in', function () {
    
        $is_user_logged_in = is_user_logged_in();
    
        wp_send_json_success( $is_user_logged_in );
    
        wp_die();
    
    } );
    
    //for logged-in users
    add_action( 'wp_ajax__wpso_73857697_is_user_logged_in', function () {
    
        if ( check_ajax_referer( '_ajax_nonce' ) ) {
    
            $is_user_logged_in = is_user_logged_in();
    
            wp_send_json_success( $is_user_logged_in );
    
        } else {
                        
            wp_send_json_error();
    
        };
    
        wp_die();
    
    } );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search