这是一个想法
使用 PHP 文件作为 MP3...
我知道这听起来很奇怪,但从技术上讲,PHP 文件可以解释为任何文件类型
你的前端:
<audio controls>
<source src="music.php?token=as5df65s1df" type="audio/mpeg">
</audio>
让我解释一下:
当用户访问页面时,设置一个只能使用一次的访问令牌,在src上设置,在session中设置
$_SESSION['securePlay'] = 'as5df65s1df';
if(!isset($_GET['token'])){
//No token? They tried to view the file
die('access denied');
}
if($_GET['token'] == $_SESSION['securePlay']){
// The one time token hasn't been used yet
// Go ahead and mimic an MP3 and play the music
$file = 'realMusic.mp3';
if (file_exists($file)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
exit;
}
//Here is where you'll want to invalidate the current token and get a new one
} else {
//The tokens don't match meaning you got a new one and the old one cant be used anymore
die('access denied');
}
这将执行以下操作:
当用户访问页面时,点击播放,它可以工作,因为使用了令牌
当用户访问音频源页面时,未设置获取令牌,访问被拒绝
当用户访问音频 src 页面时,get 已经在初始加载时加载,因此它已过期
如果有人使用inspect查看src文件,然后右键保存源码,说明token已经被使用,拒绝访问
这不是一个防弹系统,但它对普通网站来说非常有用
如果是有效播放,则 php 文件将显示为 mp3 文件
如果不是有效播放,则显示为 php 文件并拒绝访问
您还需要将 mp3 文件放在公共文件夹之外,以便只有 php 文件阅读器可以访问它们。客户端永远不会看到 .mp3 文件,如果您决定将 mp3 文件置于公共视图中,他们将不得不猜测查看路径。
我的建议是使用 JS 音频 api,每次你想播放歌曲时,创建一个新的音频对象并通过 AJAX 获取一个新的令牌
注意:在获取新令牌时,请使用 POST 或除获取之外的任何其他方式,我们希望避免客户端手动转到该路由并获取令牌