php 关于`$HTTP_RAW_POST_DATA` 被弃用的警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26261001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Warning about `$HTTP_RAW_POST_DATA` being deprecated
提问by rr-
I switched to PHP 5.6.0 and now I get the following warning everywhere:
我切换到 PHP 5.6.0,现在到处都收到以下警告:
Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated and will
be removed in a future version. To avoid this warning set
'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream
instead. in Unknown on line 0
Warning: Cannot modify header information - headers already sent in Unknown on line 0
Fine, I rely on some deprecated feature. Except that I don't!
好吧,我依赖于一些已弃用的功能。除了我没有!
- I haven't ever used this variable in any of my scripts. To be honest I had no idea it even exists.
phpinfo()
shows that I havealways_populate_raw_post_data
set to 0 (disabled). So what is going on?
- 我从来没有在我的任何脚本中使用过这个变量。老实说,我不知道它甚至存在。
phpinfo()
显示我已always_populate_raw_post_data
设置为 0(已禁用)。那么发生了什么?
I don't want to "avoid the warning" by setting this value to -1. This will just hide the warning, and I'll still have deprecated configuration. I want to solve the problem at its source and know why PHP thinks that HTTP_RAW_POST_DATA
populating is turned on.
我不想通过将此值设置为 -1 来“避免警告”。这只会隐藏警告,我仍然会弃用配置。我想从源头上解决问题,并知道为什么 PHP 认为HTTP_RAW_POST_DATA
填充已打开。
回答by rr-
It turns out that my understanding of the error message was wrong. I'd say it features very poor choice of words. Googling around shown me someone else misunderstood the message exactly like I did - see PHP bug #66763.
原来我对错误信息的理解是错误的。我会说它的用词选择很差。谷歌搜索显示其他人完全像我一样误解了该消息 - 请参阅PHP 错误 #66763。
After totally unhelpful "This is the way the RMs wanted it to be." response to that bug by Mike, Tyrael explains that setting it to "-1" doesn't make just the warning to go away. It does the right thing, i.e. it completely disables populating the culprit variable. Turns out that having it set to 0 STILLpopulates data under some circumstances. Talk about bad design! To cite PHP RFC:
在完全没有帮助之后“这就是 RM 想要的样子。” 对 Mike 的那个 bug 的回应,Tyrael 解释说,将它设置为“-1”并不仅仅是警告消失。它做正确的事,即它完全禁止填充罪魁祸首变量。事实证明,在某些情况下将其设置为 0 STILL 会填充数据。谈论糟糕的设计!引用PHP RFC:
Change always_populate_raw_post_data INI setting to accept three values instead of two.
- -1: The behavior of master; don't ever populate $GLOBALS[HTTP_RAW_POST_DATA]
- 0/off/whatever: BC behavior (populate if content-type is not registered or request method is other than POST)
- 1/on/yes/true: BC behavior (always populate $GLOBALS[HTTP_RAW_POST_DATA])
更改 always_populate_raw_post_data INI 设置以接受三个值而不是两个。
- -1:master的行为;永远不要填充 $GLOBALS[HTTP_RAW_POST_DATA]
- 0/off/whatever:BC 行为(如果内容类型未注册或请求方法不是 POST 则填充)
- 1/on/yes/true:BC 行为(总是填充 $GLOBALS[HTTP_RAW_POST_DATA])
So yeah, setting it to -1 not only avoids the warning, like the message said, but it also finally disablespopulating this variable, which is what I wanted.
所以是的,将它设置为 -1 不仅可以避免警告,就像消息所说的那样,而且最终还禁用了填充这个变量,这正是我想要的。
回答by Eat Ong
Been awhile until I came across this error. Put up my answer for anyone who may stumble upon this issue.
过了一段时间,直到我遇到这个错误。为任何可能偶然发现此问题的人提供我的答案。
The error only means that you are sending an empty POST request. This error is commonly found on HTTPRequests with no parameters passed. To avoid this error, you can always add a parameter to the POST without changing the php.ini.
该错误仅表示您正在发送一个空的 POST 请求。此错误通常出现在没有传递参数的 HTTPRequest 上。为避免此错误,您始终可以在不更改 php.ini 的情况下向 POST 添加参数。
Like:
喜欢:
$.post(URL_HERE
,{addedvar : 'anycontent'}
,function(d){
doAnyHere(d);
}
,'json' //or 'html','text'
);
回答by Sebastian Sulinski
I experienced the same issue on nginx server (DigitalOcean) - all I had to do is to log in as root
and modify the file /etc/php5/fpm/php.ini
.
我在 nginx 服务器(DigitalOcean)上遇到了同样的问题——我所要做的就是以身份登录root
并修改文件/etc/php5/fpm/php.ini
。
To find the line with the always_populate_raw_post_data
I first run grep
:
要找到always_populate_raw_post_data
我第一次运行的行grep
:
grep -n 'always_populate_raw_post_data' php.ini
That returned the line 704
那返回了行 704
704:;always_populate_raw_post_data = -1
Then simply open php.ini
on that line with vi
editor:
然后只需php.ini
在该行上用vi
编辑器打开:
vi +704 php.ini
Remove the semi colon to uncomment it and save the file :wq
删除分号以取消注释并保存文件 :wq
Lastly reboot the server and the error went away.
最后重新启动服务器,错误消失了。
回答by Ricardo Tribaldos
If you are using WAMP...
如果您使用的是WAMP...
you should add or uncomment the property always_populate_raw_post_data
in php.ini
and set its value to -1
. In my case php.ini
is located in:
你应该添加或取消对财产always_populate_raw_post_data
中php.ini
和其值设置为-1
。在我的情况下php.ini
位于:
C:\wamp64\bin\php\php5.6.25\php.ini
C:\wamp64\bin\php\php5.6.25\php.ini
..but if you are still getting the warning (as I was)
You should also set
always_populate_raw_post_data = -1
inphpForApache.ini
:
C:\wamp64\bin\php\php5.6.25\phpForApache.ini
If you can't find this file, open a browser window and go to:
http://localhost/?phpinfo=1
and look for the value of Loaded Configuration Filekey. In my case the
php.ini
used by WAMP is located in:
C:\wamp64\bin\apache\apache2.4.23\bin\php.ini
(symlink to C:\wamp64\bin\php\php5.6.25\phpForApache.ini)
..但是如果你仍然收到警告(就像我一样)
你也应该设定
always_populate_raw_post_data = -1
在phpForApache.ini
:
C:\wamp64\bin\php\php5.6.25\phpForApache.ini
如果找不到此文件,请打开浏览器窗口并转到:
http://localhost/?phpinfo=1
并查找Loaded Configuration File键的值。在我的情况下
php.ini
,WAMP 使用的位于:
C:\wamp64\bin\apache\apache2.4.23\bin\php.ini
(符号链接到 C:\wamp64\bin\php\php5.6.25\phpForApache.ini)
Finally restart WAMP (or click restart all services)
最后重启WAMP(或者点击重启所有服务)
回答by Shaan Ansari
If the .htaccess
file not avilable create it on root folder and past this line of code.
如果.htaccess
文件不可用,则在根文件夹中创建它并通过这行代码。
Put this in .htaccess
file (tested working well for API)
将其放入.htaccess
文件中(经测试适用于 API)
<IfModule mod_php5.c>
php_value always_populate_raw_post_data -1
</IfModule>
回答by Nava Bogatee
Uncommenting the
取消注释
always_populate_raw_post_data = -1
in php.ini ( line# 703 ) and restarting APACHE services help me get rid from the message anyway
在 php.ini ( line# 703 ) 中并重新启动 APACHE 服务帮助我摆脱消息
; Always populate the $HTTP_RAW_POST_DATA variable. PHP's default behavior is
; to disable this feature and it will be removed in a future version.
; If post reading is disabled through enable_post_data_reading,
; $HTTP_RAW_POST_DATA is *NOT* populated.
; http://php.net/always-populate-raw-post-data
; always_populate_raw_post_data = -1
回答by Grirg
For anyone still strugling with this problem after changing the php.init as the accepted answer suggests. Since the error ocurs when an ajax petition is made via POST
without any parameter all you have to do is change the send method to GET
.
对于在按照公认的答案更改 php.init 后仍然在为这个问题而苦苦挣扎的任何人。由于在POST
没有任何参数的情况下通过 ajax 请愿时发生错误,因此您所要做的就是将 send 方法更改为GET
.
var xhr = $.ajax({
url: url,
type: "GET",
dataType: "html",
timeout: 500,
});
Still an other option if you want to keep the method POST
for any reason is to add an empty JSON object to the ajax petititon.
如果您POST
出于任何原因想保留该方法,还有另一种选择是向 ajax petititon 添加一个空的 JSON 对象。
var xhr = $.ajax({
url: url,
type: "POST",
data: {name:'emtpy_petition_data', value: 'empty'}
dataType: "html",
timeout: 500,
});
回答by user9541305
I got this error message when sending data from a html form (Post method). All I had to do was change the encoding in the form from "text/plain" to "application/x-www-form-urlencoded" or "multipart/form-data". The error message was very misleading.
从 html 表单(Post 方法)发送数据时收到此错误消息。我所要做的就是将表单中的编码从“text/plain”更改为“application/x-www-form-urlencoded”或“multipart/form-data”。错误信息非常具有误导性。
回答by Jay Dadhania
Unfortunately, this answer here by @EatOng is not correct. After reading his answer I added a dummy variable to every AJAX request I was firing (even if some of them already had some fields) just to be sure the error never appears.
不幸的是,@EatOng 的这个答案是不正确的。阅读他的回答后,我向我触发的每个 AJAX 请求添加了一个虚拟变量(即使其中一些已经有一些字段),以确保错误永远不会出现。
But just now I came across the same damn error from PHP. I double-confirmed that I had sent some POST data (some other fields too along with the dummy variable). PHP version 5.6.25
, always_populate_raw_post_data
value is set to 0
.
但刚才我遇到了同样该死的 PHP 错误。我再次确认我已经发送了一些 POST 数据(一些其他字段以及虚拟变量)。PHP 版本5.6.25
,always_populate_raw_post_data
值设置为0
.
Also, as I am sending a application/json
request, PHP is not populating it to $_POST
, rather I have to json_decode()
the raw POST request body, accessible by php://input
.
另外,因为我发送一个application/json
请求,PHP是不是填充到$_POST
,而我得json_decode()
生,POST请求主体,通过访问php://input
。
As the answer by @rr- cites,
正如@rr- 引用的答案,
0/off/whatever: BC behavior (populate if content-type is not registeredor request method is other than POST).
0/off/whatever:BC 行为(如果content-type 未注册或请求方法不是 POST则填充)。
Because the request method is for sure POST, I guess PHP didn't recognize/like my Content-Type: application/json
request (again, why??).
因为请求方法肯定是 POST,所以我猜 PHP 没有识别/喜欢我的Content-Type: application/json
请求(再说一次,为什么??)。
OPTION 1:
选项1:
Edit the php.ini
file manually and set the culprit variable to -1
, as many of the answers here suggest.
php.ini
手动编辑文件并将罪魁祸首变量设置为-1
,正如这里的许多答案所建议的那样。
OPTION 2:
选项 2:
This is a PHP 5.6 bug. Upgrade PHP.
这是一个 PHP 5.6 错误。升级 PHP。
OPTION 3:
选项 3:
As @user9541305 answered here, changing the Content-Type
of AJAX request to application/x-www-form-urlencoded
or multipart/form-data
will make PHP populate the $_POST
from the POSTed body (because PHP likes/recognizes those content-type
headers!?).
正如@user9541305 在这里回答的那样,将Content-Type
AJAX 请求的更改为application/x-www-form-urlencoded
或multipart/form-data
将使 PHP$_POST
从 POSTed 正文中填充(因为 PHP 喜欢/识别这些content-type
标头!?)。
OPTION 4: LAST RESORT
选项 4:最后的手段
Well, I did not want to change the Content-Type
of AJAX, it would cause a lot of trouble for debugging. (Chrome DevTools nicely views the POSTed variables of JSON requests.)
好吧,我不想改变Content-Type
AJAX的,它会给调试带来很多麻烦。(Chrome DevTools 可以很好地查看 JSON 请求的 POSTed 变量。)
I am developing this thing for a client and cannot ask them to use latest PHP, nor to edit the php.ini file. As a last resort, I will just check if it is set to 0
and if so, edit the php.ini
file in my PHP script itself. Of course I will have to ask the user to restart apache. What a shame!
我正在为客户开发这个东西,不能要求他们使用最新的 PHP,也不能编辑 php.ini 文件。作为最后的手段,我将检查它是否设置为0
,如果设置为,则php.ini
在我的 PHP 脚本本身中编辑该文件。当然,我将不得不要求用户重新启动 apache。多可惜!
Here is a sample code:
这是一个示例代码:
<?php
if(ini_get('always_populate_raw_post_data') != '-1')
{
// Get the path to php.ini file
$iniFilePath = php_ini_loaded_file();
// Get the php.ini file content
$iniContent = file_get_contents($iniFilePath);
// Un-comment (if commented) always_populate_raw_post_data line, and set its value to -1
$iniContent = preg_replace('~^\s*;?\s*always_populate_raw_post_data\s*=\s*.*$~im', 'always_populate_raw_post_data = -1', $iniContent);
// Write the content back to the php.ini file
file_put_contents($iniFilePath, $iniContent);
// Exit the php script here
// Also, write some response here to notify the user and ask to restart Apache / WAMP / Whatever.
exit;
}
回答by Martins
Well, if there's anyone out there on a shared hosting and with no access to php.ini
file, you can set this line of code at the very top of your PHP files:
好吧,如果有人在共享主机上并且无法访问php.ini
文件,则可以在 PHP 文件的最顶部设置这行代码:
ini_set('always_populate_raw_post_data', -1);
Works more of the same. I hope it saves someone some debugging time :)
工作原理相同。我希望它可以为某人节省一些调试时间:)