Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果条件为真,如何使用PHP播放视频_Php - Fatal编程技术网

如果条件为真,如何使用PHP播放视频

如果条件为真,如何使用PHP播放视频,php,Php,此文件接受密码输入 <html> <form action="playvideo.php" method="post"> <input type="text" name="password"/> <input type="submit"> </html> 此文件播放视频 <?php if(isset($_POST["password"])){ $password=$_POST["password"];

此文件接受
密码
输入

<html>
   <form action="playvideo.php" method="post">
   <input type="text" name="password"/>
   <input type="submit">
</html>

此文件播放视频

<?php
if(isset($_POST["password"])){
   $password=$_POST["password"];
}
else{
   $password="";    
}
if($password!="pass"){  
   echo "unauthorised";
}
else{
   $local_file = 'video.mp4';
   $size = filesize($local_file);
   header("Content-Type: video/mp4");
   header("Content-Length: ".$size);
   readfile($local_file);
}
?>


如果我不将代码封装在
if
语句中,代码也可以正常工作。

只需进行少量更改

你可以试试看

<?php
 if((isset($_POST["password"])) && (!empty($_POST['password']))) {
   $password=$_POST["password"]; //recommended to sanitize this input value
      if($password!="pass"){  
          echo "unauthorised";
      }else{
          $local_file = 'video.mp4';
          $size = filesize($local_file);
          header("Content-Type: video/mp4");
          header("Content-Length: ".$size);
          readfile($local_file);
      }
}else{
    echo 'password is not provided';
}
?>

password.html

<html>
<form action="playvideo.php" method="post">
<input type="text" name="password"/>
<input type="submit">
</html>

playvideo.php

<?php
  $password = ($_SERVER['REQUEST_METHOD'] === 'POST' AND isset($_POST['password'])) ? $_POST['password'] : '';
  if( $password !== 'pass')
  {
    echo 'unauthorized';
    // stop execution
    exit;
  }

  // Now only for authorized users
  $local_file = 'video.mp4';
  $size = filesize($local_file);
  header("Content-Type: video/mp4");
  header("Content-Length: ".$size);
  readfile($local_file);
  exit;
<?php
  session_start();

  $hiddenPassword = 'pass';      
  if($_SERVER['REQUEST_METHOD'] === 'POST')
  {
      if(isset($_POST['password']) AND $_POST['password'] === $hiddenPassword)
      {
        $_SESSION['authorized'] = TRUE;
      }
  }

  if(isset($_SESSION['authorized']) AND $_SESSION['authorized'])
  {
    $local_file = 'video.mp4';
    $size = filesize($local_file);
    header("Content-Type: video/mp4");
    header("Content-Length: ".$size);
    readfile($local_file);
    exit;
  }
  else
  {
    echo 'Not authorized';
    exit;
  }

很抱歉,您的浏览器不支持视频元素。

您有什么问题?我想用密码保护我的视频。无效源。如果我删除if-else语句,视频在没有直接URL保护的情况下可以正常播放。它仍然只是显示视频缓冲,然后停止说无效的url。
无效的url
似乎与此更相关,而不是无效的密码。抱歉,它说的是无效的源如何播放-通过浏览器或External player?我正在通过浏览器播放
<?php  
  $hiddenPassword = 'pass';

  if($_SERVER['REQUEST_METHOD'] === 'POST')
  {
      if(isset($_POST['password']) AND $_POST['password'] === $hiddenPassword)
      {
        header('Location: playvideo.php?pass=' . hash('sha512', $hiddenPassword));
        exit;
      }
  }
  else if(isset($_GET['pass']) AND $_GET['pass'] == hash('sha512', $hiddenPassword))
  {
    $local_file = 'video.mp4';
    $size = filesize($local_file);
    header("Content-Type: video/mp4");
    header("Content-Length: ".$size);
    readfile($local_file);
    exit;
  }
  else
  {
    die('Not authorized :(');
  }
<?php
if ((isset($_POST["password"])) && (!empty($_POST['password']))) {
    $password = trim($_POST["password"]); 
    if ($password != "pass") {
        echo "Unauthorised";
    } else {
        ?>
        <video width="320" height="240" controls autoplay>  <!--Video tag introduced in HTML5-->
            <source src="video.mp4" type="video/mp4">
            Sorry, your browser doesn't support the video element.
        </video>
        <?php
    }
} else {
    echo 'Password is not provided';
}
?>