我正在写一个反窃取的下载脚本,我的计划是创建一个临时文件,该文件以会话ID命名,然后在会话到期后,该文件将被自动删除。有可能吗?你能给我一些如何在PHP中做到这一点的技巧吗?
非常感谢您的回复
发布于 2009-11-23 07:52:02
好的,到目前为止我们有以下需求
让我们看看。这是不能工作的代码,但它应该按照以下方式工作:
<?php // download.php
session_start(); // start or resume a session
// always sanitize user input
$fileId = filter_input(INPUT_GET, 'fileId', FILTER_SANITIZE_NUMBER_INT);
$token = filter_input(INPUT_GET, 'token', FILTER_UNSAFE_RAW);
$referer = filter_input(INPUT_SERVER, 'HTTP_REFERER', FILTER_SANITIZE_URL);
$script = filter_input(INPUT_SERVER, 'SCRIPT_NAME', FILTER_SANITIZE_URL);
// mush session_id and fileId into an access token
$secret = 'i can haz salt?';
$expectedToken = md5($secret . session_id() . $fileId);
// check if request came from download.php and has the valid access token
if(($expectedToken === $token) && ($referer === $script)) {
$file = realpath('path/to/files/' . $fileId . '.zip');
if(is_readable($file)) {
session_destroy(); // optional
header(/* stuff */);
fpassthru($file);
exit;
}
}
// if no file was sent, send the page with the download link.
?>
<html ...
<?php printf('a href="/download.php?fileId=%s&token=%s',
$fileId, $expectedToken); ?>
...
</html>就是这样。不需要数据库。这应该涵盖需求1-3。你不能用PHP控制速度,但是如果你在发送文件后没有破坏会话,你可以在会话中写一个计数器,限制用户在会话期间发送的文件数量。
我全心全意地同意,这可以比这个monkeyform hack更优雅地解决,但作为概念证明,它应该足够了。
https://stackoverflow.com/questions/1779205
复制相似问题