Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

PHP $_POST is empty even though the form or client sent data

Error$_POST is an empty array

Short answer

PHP fills $_POST only for application/x-www-form-urlencoded and multipart/form-data. A JSON body needs php://input. $_POST is also empty when the body is larger than post_max_size, when enable_post_data_reading is off, or when a redirect turned the POST into a GET. Check the request's Content-Type first.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Dump what PHP received: the request method, the content type, and the raw body size. This shows which cause applies.

    <?php
    var_dump($_SERVER['REQUEST_METHOD'], $_SERVER['CONTENT_TYPE'] ?? null, $_SERVER['CONTENT_LENGTH'] ?? null, $_POST);
    var_dump(strlen(file_get_contents('php://input')));
  2. 2Match what you see, then jump to its fix

    1. 1CONTENT_TYPE is application/json and the raw input contains your data.The body is JSON, not form dataFix
    2. 2CONTENT_LENGTH is larger than post_max_size (default 8M); $_POST and $_FILES are both empty, usually on a file upload.The request body is bigger than post_max_sizeFix
    3. 3$_POST is empty for every request, even small form posts, and the raw input has the data.enable_post_data_reading is turned offFix
    4. 4REQUEST_METHOD is GET in the dump, although the form used POST, and the URL was redirected (for example http to https).A redirect changed the POST into a GETFix

What does the error look like?

  • $_POST is an empty array
  • var_dump($_POST) prints array(0) {}

Cause 1 of 4

The body is JSON, not form data

1Check

$_POST only works with application/x-www-form-urlencoded or multipart/form-data. Any other content type, such as JSON, is not parsed into it.

curl -s -X POST -H "Content-Type: application/json" -d '{"name":"a"}' https://example.com/api.php

2Fix

Read the raw body and decode it.

$data = json_decode(file_get_contents('php://input'), true);

3Verify

The decoded array contains your fields.

var_dump($data);

Confirmed by php.net

Did this fix it?

Cause 2 of 4

The request body is bigger than post_max_size

1Check

If the post data is greater than post_max_size, $_POST and $_FILES are empty and PHP raises no error.

php -r "echo ini_get('post_max_size');"

2Fix

Raise post_max_size above upload_max_filesize in php.ini (it is a per-directory setting, so ini_set() at runtime does not work), then restart PHP.

; php.ini
post_max_size = 64M
upload_max_filesize = 32M

3Verify

Repeat the upload; the arrays are filled.

php -r "echo ini_get('post_max_size');"

Confirmed by php.net

Did this fix it?

Cause 3 of 4

enable_post_data_reading is turned off

1Check

With this option disabled, $_POST and $_FILES are not populated and the body stays available in php://input.

php -r "var_dump(ini_get('enable_post_data_reading'));"

2Fix

Remove the override or set it back to 1, unless you really want to parse the body yourself.

; php.ini
enable_post_data_reading = 1

3Verify

A normal form post fills $_POST.

curl -s -d "name=a" https://example.com/form.php

Confirmed by php.net

Did this fix it?

Cause 4 of 4

A redirect changed the POST into a GET

1Check

For a 301 the user agent may change the method from POST to GET. A 308 does not allow that.

curl -sI -X POST http://example.com/form.php

2Fix

Post straight to the final URL, or make the redirect a 308 instead of a 301.

# the form action should already be the final https address
<form action="https://example.com/form.php" method="post">

3Verify

REQUEST_METHOD is POST and $_POST has the fields.

curl -s -d "name=a" https://example.com/form.php

Confirmed by rfc-editor.org

Did this fix it?

How do I stop it coming back?

  • Send the correct Content-Type and read the body with php://input for JSON APIs.
  • Keep post_max_size above upload_max_filesize and test uploads at the largest size you allow.
  • Point forms at the final https address so no redirect sits in the way.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.