下载脚本和statcounter在不打开新页面的情况下协同工作


A download script and statcounter working together without opening a new page

我运行了这个下载脚本,它允许人们点击按钮下载文件,而不会打开任何新的html页面。一切都很好。这是剧本。

<?php
header('Content-disposition: attachment; filename=brochure-company-details.pdf');
header('Content-type: pdf');
readfile('brochure-company-details.pdf');
?>

然而,我真的希望能够同时运行statcounter脚本,以便记录谁在下载文件。当一个html页面打开并运行它的函数时,Statcounter脚本工作得很好。但我不想打开新的页面。我只想开始下载。用户体验是,他们只有文件下载,而不知道statcounter会记录事件。

你能帮忙吗?

您想要的是不可能的。(在pdf下载中执行javascript)Statcounter是一个脚本,由浏览器通过包含在html中来执行。如果你下载了一个pdf文件,你就没有执行任何js。

但是,statcounter可以看到按下了哪些链接,因此您可以找到下载的文件;只要您的文件是通过使用包含statcounter的html上的常规链接下载的。你根本不需要做任何事情,它们会被默认计算在内。

Dachi给出的想法是在您的php代码中添加一个sql插入。

<?php
$dbh = new PDO("sqlite:/path/to/database.sdb");
//Put the insertion code here. 
//Insert things like the IP, the login name, the time, etc. 
header('Content-disposition: attachment; filename=brochure-company-details.pdf');
header('Content-type: pdf');
readfile('brochure-company-details.pdf');

这也很有效,如果你不使用常规链接或不从有statcounter的html下载,这是一个很好的选择。

相关文章: