简体   繁体   English

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

[英]How to prevent people from accessing source videos without logining in?

I am trying to make a login that would give you access to some videos, pictures, and other files. 我正在尝试进行登录,以使您可以访问一些视频,图片和其他文件。 I can do this with php and a get request ( www.example.com/foo?video1 ) and making sure they have a cookie of some sort, but I am unsure how to prevent someone from just typing in the link of the source files ( www.example.com/videos/video1.mp4 ). 我可以使用php和get请求( www.example.com/foo?video1 )来做到这一点,并确保它们具有某种cookie,但是我不确定如何防止某人仅输入源文件的链接( www.example.com/videos/video1.mp4 )。 I need away to prevent people from accessing the source files. 我需要防止他人访问源文件。

System: 系统:

  • macOS Sierra 10.12.6 macOS Sierra 10.12.6

  • Apache 阿帕奇

Thanks, Josh 谢谢,乔希

Put the source files in some directory that is not public, or use an HTAccess. 将源文件放在非公共目录中,或使用HTAccess。 Then, use file_get_contents() and echo to spit out the video. 然后,使用file_get_contents()并回显以吐出视频。 It might look something like this: 它可能看起来像这样:

getvideo.php?video=test getvideo.php?video = test

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

Here's an idea if you're using Apache. 如果您使用的是Apache,这是一个主意。

Make sure the Rewrite module is turned on. 确保重写模块已打开。

Create a .htaccess file in your videos directory with the following content: 在视频目录中创建一个.htaccess文件,其内容如下:

RewriteEngine On

RewriteRule . video.php?video=$1

Now, create a file named video.php with something like this: 现在,创建一个名为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...
}

Now you can call yoursite.com/videos/video.mp4 and place your video files in the videos directory. 现在,您可以调用yoursite.com/videos/video.mp4并将视频文件放置在videos目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM