How to check the incoming data on the server?

0 like 0 dislike
12 views
The client using XMLHttpRequest makes a request to the server and sends the data as an array: ["string1", "string2"]

But the client can, in principle, to forge the request and does not send data or send a blank line.
How to check data on the server to filter:
1) long string less than 5 characters
2) lack of values: $x[0] == null/undefined

Outline:
$x = json_decode(file_get_contents('php://input')); if( empty($x[0]) || empty($x[1]) ) { exit(); } if( !isset($x[0]) || !isset($x[1]) ) { exit(); } //or enough? : if( strlen($x[0]) <5 || strlen($x[1]) <5 ) { exit(); }
by | 12 views

2 Answers

0 like 0 dislike
empty(0); // true
empty(null); // true
empty("); // true
empty(array()); // true

If any of these values you expect, that see else these as an option:
isset, is_null

still as the variant can be cast to the type:
if((bool)$x[0]){ // null, 0, " - will lead to false
}
by
0 like 0 dislike
by

Related questions

0 like 0 dislike
1 answer
asked Apr 21, 2019 by Alk90
0 like 0 dislike
2 answers
asked May 20, 2019 by JohnDaniels
0 like 0 dislike
3 answers
110,608 questions
257,186 answers
0 comments
33,804 users