sending POST data only works only localhost, can't figure out why...

Hi everyone,

so I can’t seem to pass POST variables to my PHP file on the server (which I have with 123.reg)

My script has the code:

private IEnumerator webTest(){
        WWWForm form = new WWWForm();
        form.AddField ("test", "null");
        UnityWebRequest www = UnityWebRequest.Post (url, form);
        yield return www.SendWebRequest ();

        if (www.isNetworkError || www.isHttpError) {
            Debug.Log (www.error);
        } else {
            Debug.Log (www.downloadHandler.text);
        }
    }

And on the server, I have a PHP file with the script:

<?PHP

if(isset($_POST["test"])){
echo "post data found";
}
else{
echo "no post data";
}

?>

The problem I am experiencing is that when I call webTest() it always returns “not post data” - it seems to work fine on localhost (easyPHP) but not on the server. I am completely stumped.

I have tested POST on the PHP file by using code like

<?PHP

$_POST["test"] = "post data";

if(isset($_POST["test"])){
echo($_POST["test"]);
}
else{
echo("no post data");
}
?>

This seems to result “post data” so that works but not when the variable is sent from unity. So I assume that the issue is on the unity end and so I am here requesting some help and advice from all you intelligent people in hope that I can cure this headache!

Any advice would be greatly appreciated! Thanks.

I think it will be null on the PHP end and if it’s null, the isset() will be false. Try something meaningful, like “1” or something. But I’m just guessing, I haven’t used PHP a long time ago and never used this in Unity at all.

You should filter and sanitize your $_POST.
Remove the isset and use this instead

$test= filter_input(INPUT_POST, 'test', FILTER_SANITIZE_SPECIAL_CHARS);

Then you can check the value of $test

sorry guys, been super busy but did eventually managed to get this fixed.

it seems you need to set .chunkedTransfer to false.

For anyone who has this issue… my code above needed the line

www.chunkedTransfer = false;

before www.sendWebRequest();