Prevent HTML5 video from being downloaded (right-click saved)?

前端 未结 20 2313
醉酒成梦
醉酒成梦 2020-11-22 15:33

How can I disable \"Save Video As...\" from a browser\'s right-click menu to prevent clients from downloading a video?

Are there more complete solutions that prevent

20条回答
  •  执笔经年
    2020-11-22 16:31

    PHP sends the html5 video tag together with a session where the key is a random string and the value is the filename.

    ini_set('session.use_cookies',1);
    session_start();
    $ogv=uniqid(); 
    $_SESSION[$ogv]='myVideo.ogv';
    $webm=uniqid(); 
    $_SESSION[$webm]='myVideo.webm';
    echo ''; 
    

    Now PHP is asked to send the video. PHP recovers the filename; deletes the session and sends the video instantly. Additionally all the 'no cache' and mime-type headers must be present.

    ini_set('session.use_cookies',1);
    session_start();
    $file='myhiddenvideos/'.$_SESSION[$_GET['video']];
    $_SESSION=array();
    $params = session_get_cookie_params();
    setcookie(session_name(),'', time()-42000,$params["path"],$params["domain"],
                                             $params["secure"], $params["httponly"]);
    if(!file_exists($file) or $file==='' or !is_readable($file)){
      header('HTTP/1.1 404 File not found',true);
      exit;
      }
    readfile($file);
    exit:
    

    Now if the user copy the url in a new tab or use the context menu he will have no luck.

提交回复
热议问题