简体   繁体   中英

how to play a html5 video only if the user is logged in?

I am listing videos in a page with a preview image. When i click on the play button, it should be played only if the user is logged in to the website. otherwise, the user should be redirected to a login page. I want to display all the videos with preview images even if the user is not logged in. So skipping the video tag if the user is not logged in is not a solution.

The website is developed using php, so a solution using php is required. Thanks in advance.

You can do this with AJAX:

First of all create a jquery method to capture the Play click event and call a PHP page passing a parameter that means you clicked on Play button:

$('#video').on('click', function(){
    var params = {playpauseclicked= true};
    // AJAX with post method
    $.post('isLogged.ajax.php', params, function(data){
        if( ! data.logged)
            //Redirect to login Page
            window.location.href = "loginPage.php";
    });
});

The isLogged.ajax.php page must get the value with $_REQUEST['logged'] and then:

<?php if (isset($_REQUEST['logged']) && $_REQUEST['playpauseclicked'] == true) {
    if(isset($_POST['logged']) && $_SESSION['loggedin'])
        echo( json_encode( $loggedin = true) );
    else
        echo(json_enconde($loggedin = false) );
?>

The following is the generic concept, how it's intended to work.

<?php if (isset($_SESSION['loggedin']) && $_SESSION['loggedin'] == true) { ?>
    //Add video tag
<?php } else { ?>
    //Redirect user to login page
<?php } ?>

Hope this helps you out.!

There is not much here to go on, but I would expect to see something like this:

foreach($videos as $video) {
    if(User::isLoggedIn()){
         // Output video thumbnail image and play button 
    } else {
         // Output video thumbnail image, but link to 'login' page
    }
}

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