Asked  7 Months ago    Answers:  5   Viewed   31 times

I have a very big html form (containing table with rows, which contain multiple inputs), which i need to submit to PHP script via POST request. The problem is some values don't come through and are absent in PHP's $_POST superglobal.

I checked (using Firebug extension) that the values are actually sent to server by the browser.

$_POST gets populated, but some values are just missing.

I checked what is raw request using:

$raw_post = file_get_contents('php://input');

and the string returned has the values. They are just not parsed into $_POST array. The strange thing i noticed is, it seems that the php://input values are cut after some length, and rest of the string does not come through to $_POST.

I thought about post_max_size and memory_limit and set them to large values:

memory_limit = 256M
post_max_size = 150M
but according to php documentation $_POST should not contain any values if request made is bigger than post_max_size.

Due to big size of form and request I cannot post it here, but i can post php script i used to debug the problem:


var_dump($file = file_get_contents('php://input'));
var_dump($_POST);
//... then i parsed the $file

Server version: Apache/2.2.9 (Debian)
PHP version: PHP 5.3.2-0.dotdeb.2

Can enyone explain reason of such strange PHP behaviour, and what should i do (change php settings, code?) to use $_POST array while processing form?

EDIT: To be clear: not only the values are missing. $_POST does not contain these keys either.

e.x. fragment of raw post:

t_dodparam%5B198%5D=&t_dodparam2%5B198%5D=&t_kolejnosc%5B198%5D=199&n_indeks=201&n_wartosc=testtesttest

Key 't_dodparam' is in post and it has key 198. The rest of parameters are missing (e.x. t_dodparam2 is in post, but it has no such key as 198, and there is no such key as n_wartosc in $_POST)

 Answers

11

PHP modifies fields containing the characters space, dot, open square bracket and others to be compatible with with the deprecated register_globals

you can find a lot of workarounds in the comments here: PHP: Variables From External Sources

For Exampe (comment by POSTer):

<?php
//Function to fix up PHP's messing up POST input containing dots, etc.
function getRealPOST() {
    $pairs = explode("&", file_get_contents("php://input"));
    $vars = array();
    foreach ($pairs as $pair) {
        $nv = explode("=", $pair);
        $name = urldecode($nv[0]);
        $value = urldecode($nv[1]);
        $vars[$name] = $value;
    }
    return $vars;
}
?>
Wednesday, March 31, 2021
 
ariel
answered 7 Months ago
66

Problem appears to be specific to PhpStorm as issue can be reproduced on another machine running windows 10

Fix

When placing test.php and test2.php into my XAMPP 7.1 localhost directory, the issue does not appear, this also resolves the issue when placing files into WAMP directory on windows 10

Wednesday, March 31, 2021
 
Valdas
answered 7 Months ago
22

A concise way to do it with your two data structures would be:

count( array_intersect( str_split($my_string), $special_chars ) )

That would also tell you how many of the special characters are in the string.

You could otherwise write a loop for your character list and manually probe with strpos.

The least effort would be converting your special character list into a regex charclass and testing against the string.

Saturday, May 29, 2021
 
rorymorris
answered 5 Months ago
33

$response contains two entries: statuses and search_metadata. You probably want to iterate through statuses, so you should loop like this:

foreach($response['statuses'] as $tweet)
{
    $text = $tweet['text'];
}

The next problem you will face with this code is $url and $hashtag - they are arrays so you can't just echo them, you have to iterate and gather only relevant info to echo.

And one more thing:

echo "<pre>". var_dump($response) ."</pre>";

var_dump does not return anything, so it can not be concatenated to <pre>. To have readable output, use it like this:

echo "<pre>";
echo var_dump($response);
echo "</pre>";
Saturday, May 29, 2021
 
Ula
answered 5 Months ago
Ula
86

First of all, if you are testing all the way from receiving the POSTed data to checking values in the database, this is not unit-test anymore : you are not testing one component in isolation of the others, but you are testing the integration of those components together.

It makes things harder to test :

  • you have to provide data in harder ways : not just as parameters to a method, but as parameters to your whole application (which means forging POST data, here, for instance)
  • you have more things to verify : not just the return value of a method, or if it threw an exception
  • you have several different and maybe unrelated things that can cause a failure (problem in some PHP code, problem in the database, database server not being available, ...), which will make failures harder to track down to find their cause.


Note that I didn't say that kind of "integration" tests is not useful, btw ;-)


Still, forging the $_POST array is quite simple : it is not read-only, and you can store whatever you want in it.

So, at the begining of your test-case, nothing prevents you from injecting any data you need in it.

Thursday, August 12, 2021
 
TMichel
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :