设置http标头时丢失会话数据(在CodeIgniter中)

时间:2021-03-18 03:48:58

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').

编辑#2好吧,问题与我原先的想法不同,所以我把这个'编辑'放在首位。我还更新了问题标题(旧标题是'来自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.

现在问题似乎与CodeIgniter会话有关。上述脚本仅在用户登录时才会运行。出于某种原因,当我手动设置响应头(使用php的header()或codeigniters输出类)时,我可以从我的日志中看到所有内容都被重新加载并重新启动 - 并且会话数据丢失,因此用户不再登录,因此脚本实际上输出了错误。

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.

我有一个mysql blob,其中包含wav格式的音频数据。我正在尝试将其输出到浏览器以流式传输浏览器想要使用的任何音频播放器插件。

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.

如果我将它指向一个物理.wav文件,音频播放器工作正常,所以这似乎不是一个问题。但是,如果我将它指向我的PHP脚本,我会得到一个“搜索合适的插件?” Firefox中的弹出窗口(无法找到任何内容),只是Chrome中一个空的非活动播放器。

Here's the PHP, the $aud object contains information retrieved from the database:

这是PHP,$ aud对象包含从数据库中检索的信息:

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.

如果我添加标题(“Content-Disposition:attachment; filename = $ name”);到上面我得下载文件,然后在外部音频播放器中播放,但这不是我想要的。

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.

这个snippit是CodeIgniter应用程序的一部分,如果这会产生影响。我设置了路由,以便/audio/$id.wav将获取适当的数据并使用上面的代码输出。

Can anyone see or think of any reason the above might not be working?

任何人都可以看到或想到上述可能无法正常工作的任何原因吗?

EDIT
These are the headers returned by the php script:

编辑这些是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

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:

为了比较,当我强制下载上述音频并直接在浏览器中打开该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

Saving and then opening the file directly does work. Having the PHP script output to the browser does not.

保存然后直接打开文件可以正常工作。将PHP脚本输出到浏览器不会。

2 个解决方案

#1


0  

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.

我将通过比较HTTP内容长度,内容类型以及Web服务器在指向物理.wav文件时发送的所有其他标头来开始调查此问题,并在尝试打开PHP脚本时发送标头。我认为实际内容正文是正确的,根据您的帖子说明如果您将文件作为附件下载,它可以与音频播放器应用程序一起播放。

#2


0  

Try adding this header in there:

尝试在其中添加此标头:

header('Content-Transfer-Encoding: binary');

标题('Content-Transfer-Encoding:binary');

See if that helps it out.

看看是否有帮助。

#1


0  

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.

我将通过比较HTTP内容长度,内容类型以及Web服务器在指向物理.wav文件时发送的所有其他标头来开始调查此问题,并在尝试打开PHP脚本时发送标头。我认为实际内容正文是正确的,根据您的帖子说明如果您将文件作为附件下载,它可以与音频播放器应用程序一起播放。

#2


0  

Try adding this header in there:

尝试在其中添加此标头:

header('Content-Transfer-Encoding: binary');

标题('Content-Transfer-Encoding:binary');

See if that helps it out.

看看是否有帮助。