简体   繁体   English

PHP REQUEST_METHOD POST 变成 GET

[英]PHP REQUEST_METHOD POST turns into GET

I'm trying to extend an old web application which uses CodeIgniter 1.7 (I know...) and am running into some trouble.我正在尝试扩展使用 CodeIgniter 1.7(我知道...)的旧 Web 应用程序,但遇到了一些麻烦。 I want a certain route to only be accessible via a POST request.我希望某个路由只能通过POST请求访问。 So at the top of the function I have the following;所以在函数的顶部,我有以下内容;

if ($_SERVER['REQUEST_METHOD'] != 'POST')
    die ('Wrong request method: ' . $_SERVER['REQUEST_METHOD']);

Locally, on PHP 7.0.14 with PHP's built-in web server, this works fine.在本地,在带有 PHP 内置 Web 服务器的 PHP 7.0.14 上,这可以正常工作。 In production, however, on a CPanel-managed server running PHP 5.4.25, it doesn't.然而,在生产中,在运行 PHP 5.4.25 的 CPanel 管理的服务器上,它没有。

When sending a POST request to my route with Postman, I get the following;使用 Postman 向我的路线发送POST请求时,我得到以下信息;

Wrong request method: GET错误的请求方法: GET

PUT , PATCH , DELETE , ... requests all get recognised correctly. PUT , PATCH , DELETE , ... 请求都被正确识别。 POST requests, however, seem to magically become GET requests.然而, POST请求似乎神奇地变成了GET请求。

POST data I sent with the request seems to disappear as well and is not to be found in either $_POST or $_GET .我随请求发送的POST数据似乎也消失了,并且在$_POST$_GET中都找不到。

It seems to not be related to the CodeIgniter framework however as when I call a file outside of the framework I get the same result.它似乎与 CodeIgniter 框架无关,但是当我在框架外调用文件时,我得到了相同的结果。

Any thoughts?有什么想法吗?

I was making requests to http://domain whcih was set up to redirect to http://www.domain , which as it turns out turns POST requests into GET requests.我正在向http://domain发出请求,设置为重定向到http://www.domain ,结果将 POST 请求转换为 GET 请求。 And Postman does not notify the user of such redirects happening.并且 Postman 不会通知用户发生此类重定向。

If you're running into this problem, make sure to check whether there's any redirects happening.如果您遇到此问题,请务必检查是否发生了任何重定向。

I wonder why this only happens with POST requests, however, and not with PUT, PATCH, DELETE, ... requests.我想知道为什么这只发生在 POST 请求中,而不发生在 PUT、PATCH、DELETE、...请求中。

Thanks to LSerni, Alex Blex and Vaviloff for pointing me in the right direction.感谢 LSerni、Alex Blex 和 Vaviloff 为我指明了正确的方向。

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

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