PHP:如何阻止对文件的直接 URL 访问,但仍允许登录用户下载它? [英] PHP: How can I block direct URL access to a file, but still allow it to be downloaded by logged in users?

查看:26
本文介绍了PHP:如何阻止对文件的直接 URL 访问,但仍允许登录用户下载它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,用户应该可以在其中登录并收听歌曲(自制的 mp3).我想让它使登录用户可以收听/下载/任何东西,并且文件应该驻留在服务器上(不存储在 MySQL 数据库中),但不能被具有路径的非用户访问到网址.

I have a website where users should be able to log in and listen to a song (a self-created mp3). I want to make it so the logged in user can listen/download/whatever, and the file should reside on the server (not be stored in the MySQL database), but not be able to be accessed by non-users who have the path to the URL.

例如:假设我的 mp3 位于 mysite.com/members/song.mp3 如果您已登录,您应该能够看到 mysite.com/members/index.php 页面,这将允许访问Song.mp3 文件.如果您未登录,mysite.com/members/index.php 页面将不会向您显示 song.mp3 文件,并且直接链接到该文件不应授予访问权限.

For example: say my mp3 is located at mysite.com/members/song.mp3 If you are logged in, you should be able to see the mysite.com/members/index.php page, which will allow access to the song.mp3 file. If you're not logged in, the mysite.com/members/index.php page will not show you the song.mp3 file, and linking directly to it should not grant access.

我很确定这是通过 htaccess 完成的,我已经做了很多谷歌搜索,并在这里搜索.我找到的两个最接近的答案是这个 htaccess 指南 http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/ 和这个 StackOverflow 问题 阻止通过 http 直接访问文件,但允许 php 脚本访问 但都没有回答我所有的问题以满足我的标准.我错过了什么?

I'm pretty sure this is done via htaccess, and I have done a lot of Googling already, and searched on here. The two closest answers I found were this htaccess guide http://perishablepress.com/press/2006/01/10/stupid-htaccess-tricks/ and this StackOverflow question Block direct access to a file over http but allow php script access but neither answer all my questions to meet my criteria. What am I missing?

推荐答案

Into folder members create new folder files,把你所有的歌曲移到这里,新建.htaccess 文件并添加以下几行:

Into folder members create new folder files, move here all your songs, create new .htaccess file and add the following lines:

Order Deny,Allow
Deny from all


在文件夹members 中创建文件get_song.php 并添加以下代码:


Into folder members create file get_song.php and add the following code:

if( !empty( $_GET['name'] ) )
{
  // check if user is logged    
  if( is_logged() )
  {
    $song_name = preg_replace( '#[^-w]#', '', $_GET['name'] );
    $song_file = "{$_SERVER['DOCUMENT_ROOT']}/members/files/{$song_name}.mp3";
    if( file_exists( $song_file ) )
    {
      header( 'Cache-Control: public' );
      header( 'Content-Description: File Transfer' );
      header( "Content-Disposition: attachment; filename={$song_file}" );
      header( 'Content-Type: application/mp3' );
      header( 'Content-Transfer-Encoding: binary' );
      readfile( $song_file );
      exit;
    }
  }
}
die( "ERROR: invalid song or you don't have permissions to download it." );


现在,您可以使用此 URL 获取歌曲文件:
http://mysite.com/members/get_song.php?name=my-song-name

And now, you can use this URL to get the song file:
http://mysite.com/members/get_song.php?name=my-song-name

这篇关于PHP:如何阻止对文件的直接 URL 访问,但仍允许登录用户下载它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆