设置 http 标头时丢失会话数据(在 CodeIgniter 中)
编辑 #2
好吧,问题与我最初的想法不同,所以我把这个“编辑”放在顶部。我还更新了问题标题(旧的问题标题是“从 mysql blob 流式传输 wav 音频”)。
现在问题似乎与 CodeIgniter 会话有关。上面的脚本只有在用户登录时才会运行。出于某种原因,当我手动设置响应头(或者 使用 php 的 header() 或 codeigniters 输出类)我可以从日志中看到所有内容都重新加载并重新初始化 - 并且会话数据丢失,因此用户不再登录,因此脚本实际上输出错误。
一旦我删除了对会话数据的任何要求或引用,音频就可以正常播放......但这并不是一个真正的选项,除非我可以设法以其他方式对用户进行身份验证。非常令人沮丧。
.
.
。
** 原文 **
我有一个 mysql blob,其中包含 wav 格式的音频数据。我试图将其输出到浏览器,以在浏览器想要使用的任何音频播放器插件中进行流式传输。
如果我将音频播放器指向物理 .wav 文件,则音频播放器可以正常工作,因此这似乎不是问题。但是,如果我将其指向我的 PHP 脚本,我会在 Firefox 中看到“搜索合适的插件?”弹出窗口(找不到任何内容),而在 Chrome 中则只有一个空的非活动播放器。
这是 PHP,$aud 对象包含从数据库检索的信息:
header("Content-length: $aud->size");
header("Content-type: audio/x-wav");
echo $aud->recording;
exit();
如果我将 header("Content-Disposition:attachment; filename=$name"); 添加到上面我下载了该文件,然后该文件在外部音频播放器中正常播放,但这不是我想要的。
这个片段是 CodeIgniter 应用程序的一部分,如果这会有所作为的话。我已经设置了路由,以便 /audio/$id.wav 将获取适当的数据并使用上面的代码输出它。
任何人都可以看到或想到上述可能不起作用的任何原因吗?
编辑
这些是 php 脚本返回的标头:
Date: Tue, 22 Mar 2011 22:12:06 GMT
Server: Apache/2.2.16 (Ubuntu)
X-Powered-By: PHP/5.3.3-1ubuntu9.3
Set-Cookie: ci_session=<long encrypted session string>; expires=Wed, 23-Mar-2011 00:12:06 GMT; path=/
Content-Length: 12345
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Content-Type: audio/x-wav
200 OK
为了进行比较,当我强制下载上述音频并直接在浏览器中打开该 wav 文件时,会返回这些标头:
Date: Tue, 22 Mar 2011 22:10:53 GMT
Server: Apache/2.2.16 (Ubuntu)
Last-Modified: Tue, 22 Mar 2011 22:08:30 GMT
Etag: "200a83-3039-49f197bfcb380"
Accept-Ranges: bytes
Content-Length: 12345
Content-Type: audio/x-wav
200 OK
保存然后直接打开文件确实工作。将 PHP 脚本输出到浏览器则不会。
EDIT #2
Ok, the problem is different to what I originally thought, so I'm putting this 'edit' up the top. I've also updated the question title (the old one was 'Streaming wav audio from a mysql blob').
The problem now seems to be related to CodeIgniter sessions. The above script will only run if the user is logged in. For some reason, when I manually set the response headers (either
with php's header() or codeigniters output class) I can see from my logs that everything gets reloaded and reinitilised -- and that the session data is lost, so the user is no longer logged in, so the script is actually outputting an error.
Once I removed any requirement or reference to session data the audio plays fine... but this isn't really an option unless I can manage to authenticate the user some other way. Very frustrating.
.
.
.
** Original Text **
I have a mysql blob which contains audio data in wav format. I'm trying to output that to the browser to stream in whatever audio player plugin the browser wants to use.
The audio player works fine if I point it at a physical .wav file, so that doesn't seem to be a problem. However, if I point it at my PHP script I get a 'Search for suitable plugin?' popup in Firefox (which fails to find anything), and just an empty inactive player in Chrome.
Here's the PHP, the $aud object contains information retrieved from the database:
header("Content-length: $aud->size");
header("Content-type: audio/x-wav");
echo $aud->recording;
exit();
If I add header("Content-Disposition: attachment; filename=$name"); to the above I get to download the file, which then plays fine in an external audio player, but that's not what I want.
This snippit is part of a CodeIgniter application, if that would make a difference. I have routing set up so that /audio/$id.wav will grab the appropriate data and output it with the code above.
Can anyone see or think of any reason the above might not be working?
EDIT
These are the headers returned by the php script:
Date: Tue, 22 Mar 2011 22:12:06 GMT
Server: Apache/2.2.16 (Ubuntu)
X-Powered-By: PHP/5.3.3-1ubuntu9.3
Set-Cookie: ci_session=<long encrypted session string>; expires=Wed, 23-Mar-2011 00:12:06 GMT; path=/
Content-Length: 12345
Keep-Alive: timeout=15, max=98
Connection: Keep-Alive
Content-Type: audio/x-wav
200 OK
And for comparison, these headers are returned when I force a download of the above audio and open that wav file directly in the browser:
Date: Tue, 22 Mar 2011 22:10:53 GMT
Server: Apache/2.2.16 (Ubuntu)
Last-Modified: Tue, 22 Mar 2011 22:08:30 GMT
Etag: "200a83-3039-49f197bfcb380"
Accept-Ranges: bytes
Content-Length: 12345
Content-Type: audio/x-wav
200 OK
Saving and then opening the file directly does work. Having the PHP script output to the browser does not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将通过比较 HTTP 内容长度、内容类型以及 Web 服务器在指向物理 .wav 文件时发送的所有其他标头与尝试打开 PHP 脚本时发送的标头来开始调查此问题。我认为实际的内容正文是正确的,根据您的帖子指出,如果您将文件作为附件下载,则可以使用音频播放器应用程序播放它。
I would start investigating this problem by comparing the HTTP content-length, content-type, and all the other headers sent by the web server when pointing to the physical .wav file, with the headers sent when trying to open the PHP script. I think the actual content body is correct, according to your post stating that if you download the file as an attachment, it can be played with an audio player application.
尝试在其中添加此标头:
header('Content-Transfer-Encoding: binary');
看看是否有帮助。
Try adding this header in there:
header('Content-Transfer-Encoding: binary');
See if that helps it out.