By rr-


2014-10-08 15:41:39 8 Comments

I switched to PHP 5.6.0 and now I get the following warning everywhere:

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!

  1. I haven't ever used this variable in any of my scripts. To be honest I had no idea it even exists.
  2. phpinfo() shows that I have always_populate_raw_post_data set to 0 (disabled). So what is going on?

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.

11 comments

@Martins 2019-07-24 19:08:20

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:

ini_set('always_populate_raw_post_data', -1);

Works more of the same. I hope it saves someone some debugging time :)

@Jay Dadhania 2019-07-15 11:33:55

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.

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.

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.

As the answer by @rr- cites,

0/off/whatever: BC behavior (populate if content-type is not registered or request method is other than POST).

Because the request method is for sure POST, I guess PHP didn't recognize/like my Content-Type: application/json request (again, why??).

OPTION 1:

Edit the php.ini file manually and set the culprit variable to -1, as many of the answers here suggest.

OPTION 2:

This is a PHP 5.6 bug. Upgrade PHP.

OPTION 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!?).

OPTION 4: LAST RESORT

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

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!

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;
}

@user9541305 2018-03-23 16:07:33

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.

@Tejaswi Tandel 2017-12-30 14:05:29

; always_populate_raw_post_data = -1 in php.init remove comment of this line .. always_populate_raw_post_data = -1

@Ravi 2017-12-30 14:18:00

could you explain?? why ? Also, format/indent your post properly.

@Shaan Ansari 2017-08-21 07:52:03

If the .htaccess file not avilable create it on root folder and past this line of code.

Put this in .htaccess file (tested working well for API)

<IfModule mod_php5.c>
    php_value always_populate_raw_post_data -1
</IfModule>

@Zohaib Hassan 2018-09-17 07:51:41

please explain My Lord

@Ricardo Tribaldos 2016-10-21 15:53:44

If you are using 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:

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 in phpForApache.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 File key. 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)

Finally restart WAMP (or click restart all services)

@Grirg 2017-02-24 11:49:12

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.

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.

var xhr = $.ajax({
   url:  url,
   type: "POST",
   data: {name:'emtpy_petition_data', value: 'empty'}
   dataType: "html",
   timeout: 500,
});

@Nava Bogatee 2016-07-30 00:51:42

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

; 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

@Eat Ong 2016-06-14 00:51:36

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.

Like:

$.post(URL_HERE
    ,{addedvar : 'anycontent'}
    ,function(d){
       doAnyHere(d);
    }
    ,'json' //or 'html','text'
);

@Craig Howell 2017-05-16 17:37:24

This is the best answer I have found for this issue! I have been dealing with this problem off and on for a month and it had me looking in the wrong direction. I simply had an empty POST on accident and once that was fixed everything worked great! Thank you for saving me from a terrible headache!

@Sebastian Sulinski 2015-09-09 13:24:44

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.

To find the line with the always_populate_raw_post_data I first run grep:

grep -n 'always_populate_raw_post_data' php.ini

That returned the line 704

704:;always_populate_raw_post_data = -1

Then simply open php.ini on that line with vi editor:

vi +704 php.ini

Remove the semi colon to uncomment it and save the file :wq

Lastly reboot the server and the error went away.

@BadHorsie 2015-12-01 13:23:22

If the line is commented out in your php.ini you are probably using a development configuration of php.ini.

@rr- 2014-10-08 15:41:39

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.

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 STILL populates data under some circumstances. Talk about bad design! To cite 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])

So yeah, setting it to -1 not only avoids the warning, like the message said, but it also finally disables populating this variable, which is what I wanted.

@srcspider 2015-04-27 14:11:56

tl;dr this is a dumb warning that appears even if you don't use the thing it warns against; set always_populate_raw_post_data to -1

@itsazzad 2015-07-28 12:01:02

i have set it always_populate_raw_post_data = -1 . still now the warning coming and corrupting the json response

@John 2016-04-15 21:20:05

So the answer in effect is to go to your php.ini file and set (or uncomment) always_populate_raw_post_data = -1.

@Andreas 2016-05-15 18:50:49

But I don't really get the point. That's exactly what the warning is saying? To avoid this warning set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead.

@rr- 2016-05-15 19:43:14

@Andreas the point is the reason why it says that, i.e. the difference between 0, which is apparently "disabled", and -1, which is... "stronger disabled"? → confusion → reason for this question (and answer).

@Andreas 2016-05-15 19:48:20

@rr-: Well, I agree that the "0 / -1" values on that setting were definitely a bad design decision back then. But the warning is apparently correct. The warning does not mention at any point that "0" means disabled? It clearly suggests to set the value to "-1".

@Randy 2019-06-29 18:47:23

Frustrating I'll say. I only started seeing this after deciding to call a PHP script via XMLHttpRequest using POST instead of GET, to avoid the data being displayed in the URL on my page. It worked fin in that the data comes through in the PHP, but now I get this warning filling my ERROR log, and setting "always_populate_raw_post_data = -1" (or "always_populate_raw_post_data = on" as suggested in my PHP.INI comments) does nothing to remove the error. I guess I'll just have to go back to exclusively using GET. Any thoughts helpful. My PHP version is 5.6. Maybe I should try upgrading to 7.x

Related Questions

Sponsored Content

4 Answered Questions

0 Answered Questions

Laravel Show error Automatically populating $HTTP_RAW_POST_DATA is deprecated

0 Answered Questions

$HTTP_RAW_POST_DATA is deprecated and php://input stream Laravel 5.2

0 Answered Questions

PHP - Curl errors

  • 2017-01-25 01:16:50
  • Zach Z
  • 31 View
  • 0 Score
  • 0 Answer
  • Tags:   php curl

4 Answered Questions

[SOLVED] $_POST Method didn't work

  • 2016-02-14 08:28:39
  • kkaka
  • 606 View
  • 1 Score
  • 4 Answer
  • Tags:   php

1 Answered Questions

[SOLVED] Deprecated: Automatically populating $HTTP_RAW_POST_DATA is deprecated

  • 2016-01-17 20:46:45
  • user4583167
  • 6988 View
  • 2 Score
  • 1 Answer
  • Tags:   php

0 Answered Questions

PHP - HTML Forms

  • 2016-10-19 20:05:41
  • Peter Ayello Wright
  • 29 View
  • 0 Score
  • 0 Answer
  • Tags:   php html forms xampp

0 Answered Questions

SQLI form submission with php error

2 Answered Questions

[SOLVED] Warning and Deprecated output API cakephp

  • 2016-06-10 04:08:41
  • user4246509
  • 155 View
  • 0 Score
  • 2 Answer
  • Tags:   php api cakephp

1 Answered Questions

[SOLVED] Laravel 5.1 and JWT-Auth Error

Sponsored Content