✭☭2018-04-27 10:25:44
PHP
✭☭, 2018-04-27 10:25:44

How to validate incoming data on the server?

The client makes a request to the server via XMLHttpRequest and sends the data as an array: ["string1", "string2"]
But the client, in principle, can fake a request and not send data or send empty strings.
How to correctly validate data on the server - to filter:
1) strings less than 5 characters long
2) missing values: $x[0] == null/undefined
Outlines:

$x = json_decode(file_get_contents('php://input'));

if( empty($x[0]) ||  empty($x[1]) )
{ exit(); }

if( !isset($x[0]) || !isset($x[1]) )
{ exit(); }

//или хватит только этого? :
if( strlen($x[0]) <5 ||  strlen($x[1]) <5  )
{ exit(); }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
ipokos, 2018-04-27
@Yertuwernat

empty(0); // true
empty(null); // true
empty(''); // true
empty(array()); // true
If you expect any of these values ​​at the input, then look at these as an option:
isset , is_null
as an option, you can cast to the type:
if((bool)$x[0]){ // null, 0 , '' - will result in false
}

A
Alexander Aksentiev, 2018-04-27
@Sanasol

php.net/manual/en/function.empty.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question