简体   繁体   English

使用PHP在LiteSpeed中设置自定义HTTP响应标头

[英]Setting custom HTTP response headers in LiteSpeed with PHP

I'm trying to set a custom HTTP 1.0 status code in order to return more descriptive error messages via AJAX (using jQuery). 我试图设置自定义HTTP 1.0状态代码,以便通过AJAX(使用jQuery)返回更多描述性错误消息。

In PHP I have something like: 在PHP中,我有类似以下内容:

header( 'HTTP/1.0 909 Hello World' );
exit;

This works as expected on my development environment using Apache 2.2.15 but it does not work on the production webserver running LiteSpeed 5.5. 这可以在使用Apache 2.2.15的开发环境上按预期方式运行,但在运行LiteSpeed 5.5的生产Web服务器上则无法运行。 LiteSpeed instead returns a 200 OK . LiteSpeed返回200 OK

Using this: 使用这个:

header( 'HTTP/1.0 404 Hello World' );
exit;

Apache returns the 404 with the text above. Apache返回上面的文本的404 LiteSpeed replaces the custom text with the default 'Not Found'. LiteSpeed将自定义文本替换为默认的“未找到”。

Any idea how to override this feature in LiteSpeed to make it work the same way as Apache? 知道如何在LiteSpeed中覆盖此功能以使其与Apache相同的方式吗? I understand they are supposed to be comparable webservers. 我了解他们应该是可比的网络服务器。

Any ideas, please do let me know. 有什么想法,请告诉我。

-P. -P。

First, why HTTP 1.0 and not 1.1? 首先,为什么要使用HTTP 1.0而不是1.1?

Second, you're trying to do things that aren't defined in the HTTP spec. 其次,您要尝试执行HTTP规范中未定义的操作。 LiteSpeed probably checks the headers its being asked to send and replaces values it doesn't recognise with some default value. LiteSpeed可能会检查要求其发送的标头,并用某些默认值替换它无法识别的值。

You shouldn't be trying to force HTTP to behave in a non-standard way, as soon as you do, it ceases to be HTTP. 您不应该试图强制HTTP以非标准的方式运行,一旦这样做,它就不再是HTTP。 The specifications are there to make sure that software implemented at different times by different people can interoperate, and how it should behave if you don't follow the spec is completely undefined. 规范可以确保由不同人员在不同时间实施的软件可以互操作,并且如果您不遵循规范,该软件应该如何运行是完全不确定的。

Apache letting a header it doesn't know through is perfectly acceptable behaviour, and so is LiteSpeed's behaviour of refusing to let it through. Apache允许不知道的标头通过是完全可以接受的行为,LiteSpeed拒绝通过标头的行为也是如此。 Making demons fly out of your nose would also be a valid thing for the server to do in this case. 在这种情况下,让恶魔从你的鼻子里飞出来也是服务器要做的一件事。 And what is the client meant to do with an HTTP response code it doesn't recognise? 客户端打算使用它无法识别的HTTP响应代码做什么?

If you need to send additional information in the header, why not add a new header? 如果您需要在标题中发送其他信息,为什么不添加新的标题? The following should do. 以下应该做。

header ('X-My-App-Header: Hello World!');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM