我运行了这个下载脚本,允许人们点击按钮并下载文件而不打开任何新的html页面。这一切都运作良好。这是脚本。
<?php
header('Content-disposition: attachment; filename=brochure-company-details.pdf');
header('Content-type: pdf');
readfile('brochure-company-details.pdf');
?>
但是,我真的希望能够同时运行statcounter脚本,以便记录下载文件的人。当一个html页面打开并运行其功能时,Statcounter脚本运行良好。但是我想要打开一个新页面。我只是想开始下载。用户体验将是他们只有文件下载,并且他们不知道统计者会记录事件。
你能帮忙吗?
答案 0 :(得分:1)
你想要的是不可能的。 (在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,那么这是一个很好的选择。