[英]Record who has downloaded what file from S3 using my PHP website
我有一个网站,允许用户使用预签名的URL从S3安全下载文件,但是我想记录谁下载了文件以及何时下载。
我尝试通过将它们重定向到另一个页面来执行此操作,然后该页面将使用一段JavaScript自动下载文件,然后将记录插入数据库表中,但是一旦脚本运行,它将停止其余页面的加载并停止它重定向回来。
我正在使用的JavaScript如下:
<script>window.location.href = “url”</script>
有可能吗?
我建议您在将文件返回给用户之前在PHP层上记录下载。 您可以从会话中获取所需的信息,例如IP地址或用户信息,将其存储在数据库中,然后将适当的标头发送回用户并开始文件下载。 您无需将用户重定向到新页面。
编辑:
例如,在您的downloads.php上,您可以:
<?php
// 1) Get the information that you would like to log
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER['REMOTE_ADDR'];
$username = $_SESSION['username'];
// ...
// 2) Store the information on your database
// For example, add a MySQL INSERT here
// ...
// 3) Return the appropriate file to the user
// Code extracted from https://stackoverflow.com/questions/6175533/
$attachment_location = $_SERVER["DOCUMENT_ROOT"] . "/file.zip";
if (file_exists($attachment_location)) {
header($_SERVER["SERVER_PROTOCOL"] . " 200 OK");
header("Cache-Control: public"); // needed for internet explorer
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: Binary");
header("Content-Length:".filesize($attachment_location));
header("Content-Disposition: attachment; filename=file.zip");
readfile($attachment_location);
die();
} else {
die("Error: File not found.");
}
有关PHP $ _SESSION和$ _SERVER的更多信息,请参见:
PHP $ _SESSION
PHP $ _SERVER
编辑2:
另一个有用的标题组合:
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
有关PHP标头的更多信息:
PHP标题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.