12

How do I go about troubleshooting this weird problem? I updated PHP on my VPS to 5.6.0 and now on this one specific script I am getting these two errors when I didn't get them before, and they really don't give me anything to go by.

<br />
<b>Deprecated</b>:  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 <b>Unknown</b> on line <b>0</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent in <b>Unknown</b> on line <b>0</b><br />

This script just runs some commands to the status of various services through shell_exec and returns the response as JSON. It doesn't doesn't use any post data, or even contain $_POST in the file. The first thing in my script is:

<?php

error_reporting(0);

header('Content-Type: application/json');

I commented out that last line, and still got the warning about modify heading information. I don't have any idea why these errors are coming up when they worked fine on the older version (which was 5.5.16)

ecnepsnai
  • 1,882
  • 4
  • 28
  • 56
  • 2
    You need to fix the first problem as described in the error message – John Conde Sep 22 '14 at 23:34
  • so what happened when you did what the message said and edited php.ini and set 'always_populate_raw_post_data' to '-1' ?? –  Sep 22 '14 at 23:37
  • 4
    This question appears to be off-topic because the answer is provided in the question itself –  Sep 23 '14 at 00:21
  • The quick&dirty solution is to set `always_populate_raw_post_data` to `-1`. The real solution is to stop relying on automatic `$HTTP_RAW_POST_DATA`. What more do you need to know? – abarnert Sep 23 '14 at 03:26
  • @abarnert On heroku I always use `php://input` but it still shows the error (I did not modify php.ini). Can `always_populate_raw_post_data` be put in the script itself rather than modifying ini? – Khurshid Alam Nov 20 '14 at 10:28
  • @KhurshidAlam: If you have a new question that's tangentially related to another question, don't ask it as a comment on that other question (_especially_ one that's been closed, but this is always true). Click the Ask Question button, and ask it as a new question. If a link to this question would be useful, click the "share" link, or just grab the URL from the address bar, and include a link to this question. – abarnert Nov 20 '14 at 19:44
  • 1
    Just encountered the same issue. Can be replicated by making a jquery post request without post data. Adding post data fixed the issue. $HTTP_RAW_POST_DATA is not used anywhere. No idea why, and dont have the time to investigate right now, but thought it worth mentioning – Steve Jan 06 '15 at 14:21
  • Steves comment seems the best solution to this if people are using jQuery. i had the same issue. was doing a $.post("url",{},function() just adding fake data in the {} worked for me. strange – WilliamStam Jan 12 '15 at 09:34
  • 4
    @WilliamStam I dug a bit further, turns out its a php 5.6 bug, that probably wont be fixed: https://bugs.php.net/bug.php?id=66763 `HTTP_RAW_POST_DATA` does not have to be used in usercode to trigger the error. Only solution is to change the ini variable, either yourself or nag your host if you dont have access. I have voted to reopen so a better answer can be provided – Steve Jan 16 '15 at 14:03
  • @Steve thanks. seems a really strange bug/"feature" tbh. – WilliamStam Jan 17 '15 at 15:06
  • Related: [Warning about `$HTTP_RAW_POST_DATA` being deprecated](http://stackoverflow.com/q/26261001/55075) at SO – kenorb Jan 15 '16 at 19:19

2 Answers2

9

You cannot use header() once text has been output to the browser. By doing as it is said in the error message:

set 'always_populate_raw_post_data' to '-1' in php.ini and use the php://input stream instead

you should get rid of those error outputs.

Adam Sinclair
  • 1,654
  • 12
  • 15
  • I don't know why you got downvoted, your answer is correct (I'll mark it correct when I can in a few moments). I figured it was something I was doing that was triggering the warning. – ecnepsnai Sep 22 '14 at 23:42
  • well its correct in it repeats exactly what the warning told you to do. - but some people i guess need to be told twice ;-) –  Sep 23 '14 at 00:11
5

$HTTP_RAW_POST_DATA is deprecated (this is causing the problem with the header)

Try this:

<?php
$postdata = file_get_contents("php://input");
?> 

Read http://php.net/manual/en/reserved.variables.httprawpostdata.php

Protomen
  • 9,471
  • 9
  • 57
  • 124