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



此文件接受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 = 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';
}
?>

需要做一些更改....

你可以尝试喜欢

<?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';
}
?>

密码.html

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

播放视频.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;

解决方案 2(带会话(

播放视频.php

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

解决方案 3 - 没有会话,但使用哈希密码和重定向。

播放视频.php

<?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 :(');
}

最新更新