【问题标题】:How to count downloaded documents如何计算下载的文件
【发布时间】:2011-12-20 14:53:54
【问题描述】:

我正在开发一个允许访问者使用此代码下载各种文档的网站:

<input type="button" class="downloadedFile" value="Download File" onclick="window.location.href=\'documents/' . $docsRow['docFileName'] . '\'" />

$docsRow['docFileName'] 是从 MySQL 数据库中检索到的每个文档的路径。

更大的图景是这些文档位于 jQuery 手风琴中,每个手风琴窗格都是一个包含一个或多个文档的类 - 像这样:

<div id="accordion">
<h3><a href="#">LEAD5220 - Leader as Change Agent</a></h3>
<div>
    <p class="className">Leader as Change Agent</p>
    <p class="classNumber">LEAD5220</p>
        <hr class="classSeparator" />
        <table width="95%">
            <tr>
                <td><strong>Document Name:</strong> Generic Annotation Format</td>
                <td width="100px" align="right"><input type="button" class="downloadedFile" value="Download File" onclick="window.location.href='documents/Generic Annotation Format.docx'" /></td>
            </tr>
            <tr>
                <td colspan="2"><strong>Description:</strong> This document can be used as a template for your annotations in the annotated bibliography forums.</td>
            </tr>
        </table>
        <hr class="classSeparator" />
        ...

我的客户希望能够计算每个文档每个班级的下载次数。

我知道我可以通过 javascript 检测按钮点击,就“每个类”部分而言,我可以使用 jQuery 来检测按钮点击的类表,但我不明白的是 1)如何将该信息获取到 PHP/MySQL 代码中以记录到数据库中,以及 2)我是否只能检测到按钮单击,或者我是否可以检测到用户是否实际将文件下载到他们的计算机(这些是 PDF、DOC、XLS文件)。

感谢大家的帮助!

【问题讨论】:

    标签: php javascript jquery download counter


    【解决方案1】:

    有 2 个选项可以计算下载次数。一种是链接到记录每次点击的 php 页面,然后重定向到实际文件。比如:

    <?
    $class = $_REQUEST['class'];
    $doc = $_REQUEST['doc'];
    
    // insert the code here to get and update the number of hits to this class/doc
    
    // now redirect
    header('Location: http://www.example.com/thefile.doc');
        ?>
    

    在 html 中,链接可以是:

    <a href="getfile.php?class=X&doc=Y">download file</a>
    

    我个人更喜欢的第二种方法是使用 ajax 记录点击次数。当用户单击链接下载时,请执行以下操作:

    $('a.thelink').click( function() {
        $.post ('getfile.php', { class: X, doc: Y });
    });
    

    这样你发送要记录的数据,用户停留在同一页面。

    如果您需要更多说明,请告诉我。

    【讨论】:

    • 呃!阿贾克斯!不像我已经在网站的其他部分大量使用它! (暂停六个月后,我才重新熟悉这个网站)谢谢,Moshe(当然还有 DaDaDom)!
    【解决方案2】:

    第二部分:你不知道。如果用户请求了该文件,他就得到了。 HTTP 是无状态的,因此一旦单击,您只能尝试检查传输给用户的金额是否等于文档的大小……但这实际上是不可能的。

    对于第一部分:不要直接链接到文档,而是链接到 PHP 页面,该页面会计算下载,尊重参数,并重定向到实际文档。或者它可以返回文档的二进制内容,无论你喜欢什么。

    【讨论】:

    • 感谢您的回复。关于检测他们是否真的下载了它,我想了很多。 - 就您对第一部分的回答而言,我不清楚您通过链接到计算下载的 PHP 页面是什么意思。你能给我更详细的解释吗?也许是一些伪代码?谢谢!
    • 查看 Moshe Shahams 的回答以获得更详细的技术解释。
    【解决方案3】:

    另一种方法是直接通过记录每次下载的php文档来服务文档。

    您将 php 文件称为 document-counter.php?file=path/file.ext

    别忘了urlencode($filename);

    文档计数器.php

    <?php
    if ( ! isset($_GET['file'])) {
        die ('_GET file not set');
    }
    
    $file = realpath($_SERVER['DOCUMENT_ROOT'] . urldecode($_GET['file']));
    if ( ! file_exists($file)) {
        die($file . ' not exists');
    }
    
    $size = filesize($imageFile);
    $extension = pathinfo ($imageFile, PATHINFO_EXTENSION);
    
    header('HTTP/1.1 200 OK');
    header('Pragma: no-cache');
    // Attention: the following line can vary depending of the extension of the file !
    header('Content-Type: application/' . $extension);
    header('Content-Length: ' . $size);
    
    // here, ***after the header***, goes all the stuff for incrementing the counters and
    // saving them
    
    readfile($imageFile);
    

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多