Javascript 记录谁使用我的PHP网站从S3下载了什么文件

Javascript 记录谁使用我的PHP网站从S3下载了什么文件,javascript,php,amazon-s3,Javascript,Php,Amazon S3,我有一个网站,允许用户使用预先签名的URL安全地从S3下载文件,但是我想记录谁下载了文件以及何时下载 我尝试过将它们重定向到另一个页面,然后使用一段JavaScript自动下载文件,然后将记录插入数据库表,但一旦脚本运行,它就会停止加载页面的其余部分,停止重定向回页面 我使用的JavaScript如下所示: <script>window.location.href = “url”</script> window.location.href=“url” 有可能吗?我建议

我有一个网站,允许用户使用预先签名的URL安全地从S3下载文件,但是我想记录谁下载了文件以及何时下载

我尝试过将它们重定向到另一个页面,然后使用一段JavaScript自动下载文件,然后将记录插入数据库表,但一旦脚本运行,它就会停止加载页面的其余部分,停止重定向回页面

我使用的JavaScript如下所示:

<script>window.location.href = “url”</script>
window.location.href=“url”

有可能吗?

我建议您在将文件返回给用户之前,将下载记录在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标题的更多信息:

我建议您在将文件返回给用户之前,将下载记录在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标题的更多信息:

为什么不启用S3访问日志并处理这些日志?@MitchDempsey这些日志不会有帮助,因为它们只会记录访问S3存储桶的帐户,不是数据库中的用户帐户为什么不启用S3访问日志并处理这些日志?@MitchDempsey这些日志没有帮助,因为它们只记录访问S3存储桶的帐户,不是数据库中的用户帐户我对这个还是相当陌生的,我不确定你想要什么mean@NickFallows我添加了一个快速的例子,可以帮助您理解我的意思…我还尝试使用未签名的url下载它,它会返回corrupt@NickFallows您可能没有为文件使用正确的头组合。我建议你浏览一下这个链接:我对这个还是相当陌生的,我不知道你在想什么mean@NickFallows我添加了一个快速的例子,可以帮助您理解我的意思…我还尝试使用未签名的url下载它,它会返回corrupt@NickFallows您可能没有使用正确的页眉组合进行编辑文件我建议您浏览以下链接: