PHP Post is corrupt

Hi,

I do an ajax POST request to my own PHP backend. IT works on my developer machine and it works on the production machine but it does not work on the stage machine. All with the same data. So I guess it must have something to do with the PHP configuration.

The request looks completly fine in developer tools

but the response is

when i try to access $_POST[‘token’].

When i log the $_POST in PHP it is an empty array

array(0) {
}

when i log the php input with

writeDebug(file_get_contents(“php://input”));

its also empty.

The size of the data is 16428 bytes. So nothing max_post_size could not fit into.

I am searching for the reason for over 4 hours now and have no more ideas where to continue

Any input is welcome.

What is the php $_SERVER[‘REQUEST_METHOD’] value? I suspect the server is being reached via a redirect, so it’s getting a get method request w/o any post data.

No, the server is reached directly without any proxy, or similar. Also it’s only this one post request. I do hundred of other post requests the same way and they work. So there must be something special on this data but I do not know what.

When I replace the data value with an empty object it works also. But the data is just a json encoded object. So I do not know why it can break the post values.

Is there a request limit? Maybe you’re hitting quota during this instance? I guess better question is, are you running the same request multiple times repeatedly or is this a single request per few seconds situation?

No it’s a single request triggered by a button press of the user,

Can you put the browser network calls?. Something like this:

Is it only this request that doesn’t work, or do all requests not work?

What server is running PHP, does it maybe have limitations on POST size?

All three server have same config with php8.2 and Apache 2.6. it works on two servers but not on the third. All requests work only this one does not work. It must be a combination of these special request and the config of the server. max_post_size is set to 20M. So that not the reason

Maybe run phpinfo() on dev and staging, save the HTML from both in files locally and then compare them using diff?

1 Like

It could be the max_input_vars ini setting. Though I wonder why anyone would set that to less than 4.

As @Zensei mentioned, you need to take a look at your network requests. Chances are you’re getting a 302 to your POST request, and the server is then sending a GET request to the same URL…hence you have no POST data.

First thing you ALWAYS do when you have an error is look in the logs 9/10 you’ll solve the problem this way. When you get a chance tune up your logs to make sure any messages being send are verbose.

Not for certain, but sounds like a 302.

As a check, echo the $_SERVER[‘REQUEST_METHOD’] before the very first line of code, and die. If it is GET, the redirect is happening before it touches the code, which means the issue is possibly a server issue.

One more thing to rule out…I’ve had this happen in Edge, then when switching to Chrome all was fine. Switching back to Edge the problem reappeared. Turn off all browser extensions and retest.

Not necessarily. While I agree you should always check your logs, what I quoted isn’t always true. A 302 redirect doesn’t necessarily mean it’s a bad thing. The issue @Thallius is having doesn’t pertain to network issues.

The error is stating that the key token doesn’t exist within the $_POST array. Now I wouldn’t think Thallius wouldn’t know the difference between POST and GET. So we can rule out the use of GET and POST methods in the form. We’ve already ruled that there is no request limit with my question since what I was trying to get at is, if this is using a 3rd party API, there most likely will be a quota limit to prevent DDOS.


The next logical question would be what are these payloads coming in as? JSON or www-form-urlencoded? I’ve done something similar multiple times where my APIs are using php://input and expecting it to be in a JSON format, but I’m sending data via www-form-urlencoded and I don’t see indexes I’m expecting. Or it could just be as simple as what @rpkamp is suggesting and could just be a misconfiguration in the php.ini file.

The data is send as www-form-urlencoded. That’s why I json encode the value of data before sending manually as I can’t sent an object otherwise.
Is there any limitation on the size of the form values?

So its definitely the length of the data field which matters.

When I send

let data =
{
    checksave : 1,
    id : 1,
    data : JSON.stringify(PMPlannersettings.settings)
}

where PMPlannersettings.settings is an array containing 3 objects which are very Large (14k when encoded), then I get an fail.

When I send

let data =
{
    checksave : 1,
    id : 1,
    data : JSON.stringify({ "1" : PMPlannersettings.settings[1], "2" : {  test : "Test" }})
}

or

let data =
{
    checksave : 1,
    id : 1,
    data : JSON.stringify({ "1" : PMPlannersettings.settings[2], "2" : {  test : "Test" }})
}

it works fine.

and

When I send

let data =
{
    checksave : 1,
    id : 1,
    data : JSON.stringify({ "1" : PMPlannersettings.settings[1], "2" :  PMPlannersettings.settings[2] })
}

it fails again

So why is the size of the data field limited?

Ok,

problem solved.

I rebooted the whole server (Linux server which is normally never rebooted).

When I just restarted https service nothing changed but after reboot of the whole server it works again fine as it did before.

I have no clue what the root cause was. So I am not very happy with this solution but what can I do…

1 Like

Have you tried turning it off and on again

3 Likes

This does happen sometimes, especially (from my own anecdotal experience) if PHP is being loaded as a FastCGI module in IIS. I’ve found i had to completely shutdown and restart the IIS instance.

Could it be because of JSON.stringify? Maybe there’s a limit on it? Though you did a server reboot and that fixed it so maybe not.