首页 > 解决方案 > PHP 使用 header('Content-Type: audio/mpeg') 时使用 file_put_contents 复制日志条目;

问题描述

我正在使用 php 播放混合的 mp3 文件。我在加载页面时一直遇到问题,它执行我的日志记录功能两次。这会导致日志中出现重复条目​​。我简化了我的代码以在此处发布。

当我执行以下 php 文件时,它会在浏览器中完美播放“test.mp3”。我的 error_log 中没有错误。

    <?php
header('Content-type: audio/mpeg');
header('X-Pad: avoid browser bug');
header('Cache-Control: no-cache');

print readfile("test.mp3");

$logentry = date("l jS \of F Y h:i:s A") . " Play Successful\n";

file_put_contents('play.log', $logentry, FILE_APPEND);
    
?> 

在加载上述 php 一次后查看我的 play.log 文件,我得到以下信息:

Monday 24th of August 2020 03:57:10 AM Play Successful
Monday 24th of August 2020 03:57:10 AM Play Successful

重复条目。为了进一步隔离,我注释掉了“header('Content-type: audio/mpeg');” 行和网页按预期加载二进制 mp3 文件(一堆随机字符)。当我检查日志文件时,我看到它按应有的方式创建了一个条目。

为什么要使用“header('Content-type: audio/mpeg');”的任何想法 导致双浸?在 Chrome 和 Edge 中加载脚本时会发生这种情况。使用 Safari,我得到了酒水条目。IE 我得到一个应有的条目。

我怎样才能使这项工作在所有浏览器中可靠地工作以获得准确的日志?

标签: phpaudiocross-browsermp3

解决方案


推荐阅读