如何在不登录的情况下阻止人们访问源视频?

时间:2017-12-22 20:34:53

标签: php apache authentication

我正在尝试登录,以便您访问某些视频,图片和其他文件。我可以使用php和get请求(www.example.com/foo?video1)并确保它们有某种cookie,但我不确定如何阻止某人只输入源文件的链接({{1 }})。我需要远离以防止人们访问源文件。

系统:

  • macOS Sierra 10.12.6

  • 的Apache

谢谢, 约什

2 个答案:

答案 0 :(得分:0)

将源文件放在某个非公共目录中,或使用HTAccess。 然后,使用file_get_contents()和echo来吐出视频。 它可能看起来像这样:

getvideo.php?视频=测试

    <?php
if($_SESSION["username"]!==null)//whatever auth mechanism
{
if(file_exists("videos/".  str_replace( [ '\\', '.', '/'], '', $_GET["video"] ). ".mp4"))
{
header("Content-Type: video/mp4");
readfile("videos/".  str_replace( [ '\\', '.', '/'], '', $_GET["video"] ). ".mp4");
}

else
{
header("HTTP/1.1 404 Not Found");
}
}
else
{
header("HTTP/1.1 401 Unauthorized");
}
exit;
?>

答案 1 :(得分:0)

如果你使用的是Apache,那就是一个想法。

确保Rewrite module已开启。

使用以下内容在视频目录中创建.htaccess文件:

RewriteEngine On

RewriteRule . video.php?video=$1

现在,创建一个名为video.php的文件,其内容如下:

<?php
/**
 * require your login / db files here
 * and check if the user is logged in,
 * if it isn't redirect back or whatever you need
 */

$video = $_GET['video'];
$video = str_replace('..', '', $video); // prevent ../../file 

if (file_exists($video)) {
    header('Content-type: ' . mime_content_type($video));
    echo file_get_contents($video);
} else {
    // 404...
}

现在您可以致电yoursite.com/videos/video.mp4并将您的视频文件放入视频目录。