user1119423
user1119423

Reputation: 103

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.

Upvotes: 1

Views: 1405

Answers (3)

Marcelo Machado
Marcelo Machado

Reputation: 1397

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

Upvotes: 3

MaggsWeb
MaggsWeb

Reputation: 3027

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
    }
}

Upvotes: 1

Amit Horakeri
Amit Horakeri

Reputation: 745

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.!

Upvotes: 1

Related Questions