【问题标题】:Direct link access but tracking by mysql/php?直接链接访问但通过 mysql/php 跟踪?
【发布时间】:2013-03-20 00:02:23
【问题描述】:

我有一个指向我想提供给访问者的文件的直接链接,例如:

http://www.mydomain.com/mynewmix.mp3

当上面的链接被 ping/点击/下载时,我有什么方法可以运行以下查询?

UPDATE `db` SET `downloaded`=`downloaded`+1 WHERE `url`='http://www.mydomain.com/mynewmix.mp3'

非常感谢我能得到的任何帮助。非常感谢您。

【问题讨论】:

  • 也许 ajax 可以帮到你……
  • 如果 ajax 直接转到 mp3 文件,他们将如何帮助我?

标签: php mysql .htaccess


【解决方案1】:

是的,这是可能的。您可以在 apache 中使用重写模块。所以你可以说对于所有 (mp3) 文件,服务器不应该返回 mp3 文件,而是运行一个 php 文件/脚本来执行你的查询并返回 mp3 文件。

这可能会对您有所帮助:http://www.workingwith.me.uk/articles/scripting/mod_rewrite

在您的 .htaccess 文件中:

RewriteEngine on
RewriteRule ^\.mp3$ index.php [L]

这会将所有以 .mp3 结尾的链接发送到 index.php(实际上它仍然是同一个链接,但 index.php 将被执行)

您还有其他选择:

RewriteRule ^.*/(\w)*\.mp3$ index.php?filename=$1 [L] 

这将使用带有文件名的 GET 可验证文件名执行 index.php 例如。 $_GET['filename'] = "filename"(当文件:filename.mp3)

在 index.php 中让用户下载 mp3 文件(见:Can I serve MP3 files with PHP?):

header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize("[file location eg: /home/files/sometrack.mp3]"));
header('Content-Disposition: attachment; filename="sometrack.mp3"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile("[file location eg: /home/files/sometrack.mp3]");
exit();

您可以使用以下代码动态地执行此操作:

$fileName = $_GET['filename']."mp3"; //we stripped .mp3 in the apache rewrite (might not be so smart)
$fileLocation = "/your/location/".$filename;
header("Content-Transfer-Encoding: binary"); 
header("Content-Type: audio/mpeg, audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
header('Content-length: ' . filesize($fileLocation));
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');
readfile($fileLocation);
exit();

您可以通过以下方式访问请求的链接:

$_SERVER['REQUEST_URI'];

或新的(生成的网址):

$_SERVER['REDIRECT_URL'];

【讨论】:

  • 这很接近,但我只能访问我自己的 .htaccess.... 任何我可以使用此方法获得的示例将不胜感激。
  • 看我的例子,虽然你应该自己了解它是如何工作的,否则使用它可能很危险;)
  • 这很好,但它运行 index.php 但它不允许访问者继续下载/流式传输该 mp3.... :(
  • 你的答案在这里:stackoverflow.com/questions/1516661/…
  • 谢谢乔尔...我也在这样做: RewriteRule ^.*/(\w)*\.mp3$ index.php?filename=$1 [L] 当我回显时GET,它没有在 url 栏中给我完整的链接......
【解决方案2】:

我认为还有另一种方法可以满足您的需求:

  1. 创建一个新的php文件“下载”开始下载参数 内容如下(日期和时间是获取我的 mp3 文件的关键):

    <?php
        if (isset($_GET['date']) && is_string($_GET['date']) && isset($_GET['time']) && is_string($_GET['time']))
        {
            require_once($_SERVER['DOCUMENT_ROOT'] . "/bin/functions.php");
            DownloadFile($_GET['date'],$_GET['time']);
        }
    ?>
    
  2. 使用函数“DownloadFile”创建一个不同的php文件“functions.php”:

    function DownloadFile($Date,$Time)
    {
        require('connection.php');
        mysql_select_db($database, $instance);
    
        $qry = "
            UPDATE `table` 
            SET `Field1` = `Field1` + 1
            WHERE `Field2` = '$Date' AND `Field3` = '$Time'
            ";
        $result = mysql_query($qry, $instance) or die(mysql_error());
    
        header('Content-type: Application/mp3');
        header("Content-Length: " .(string)(filesize($file)));
        header('Content-Disposition: attachment; filename=' . $file);
        readfile($file);    
    

    }

  3. 使用不同的url下载(无锚点)

http://www.yoursite.com/xxx/download.php?date=2000-01-01&time=12:00:00

【讨论】:

    【解决方案3】:

    是的,您可以这样做。点击 url 需要调用 ajax 来运行更新查询,成功后继续下载文件。

    【讨论】:

    • 如果用户直接从 url bar 访问 mp3,这将不起作用
    猜你喜欢
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    • 2017-12-18
    • 1970-01-01
    相关资源
    最近更新 更多