One of the possible ways to validate would be using the function in_array
PHP to make this check.
if (! in_array($_POST['valor'], ['opt1', 'opt2', 'opt3']) {
// inválido
}
Because someone could edit, via the developer’s tool, the value of your select
, that person could easily insert an unknown value there in their database table.
Behold:
In this example above, I could simply change the value in front-end and submit a submission.
Furthermore, there are tools that allow sending form, regardless of its definition, such as the Postman plugin, from Google Chrome. With it you can send requests to a given URL, you can pass the value you want.
So, knowing the URL, I can send what I want to your server.
That is why I strongly recommend that the validation is always done on the server, since its structure for sending the data on the client side (HTML and Javascript programming) does not guarantee the veracity of the same.
It is important to define what you want to receive on the server.
A very common error I see is the person who uses the $_GET variable to get the value of the page that will be included.
Example:
$page = $_GET['page'];
Include 'paginas/' . $page . '.php';
In the example above you obviously expect a string. But to know the level of knowledge of the programmer just pass a page[]=1
as parameter. It will not be surprising if an "array Conversion to string" appears, as this was not expected.
In such cases for lack of verification appear uglier errors, because of lack of validation.
In this case a simple filter_var
or filter_input
would solve the problem.
$page = filter_input(INPUT_GET, 'page');
if ($page === false) exit;
include 'paginas/' . $page . '.php';
Only mysql_escape_string
does not guarantee anything. I suggest formatting and validating the data always the way you want to receive.
In short: You should never rely solely on client-side validation (the browser), since everything can be manipulated.
" then you would have an unknown option there in your database table" which could be an Injection command ?
– MagicHat
@Magichat I will post a more complete example at my lunch. It’s a quieter time :p. But in advance: if the guy edit the "value" of his "option" he can send up a
array
to your backend if he wants.– Wallace Maxters
No hurry, buddy... Tks
– MagicHat
@Magichat if the guy does
name="sel[][][]"
, you may receive aarray
in an inexperienced place. I will elaborate an answer that will teach you how to usefilter_var
orfilter_input
.– Wallace Maxters
Without this filter a nut could send an "opXXXX" in the value of "sel1", like: "sel1=Queéop", then you would register in the bank as "Quequeéop", and it was not a valid option (the "validated" would be only op1, op2 and op3).
– Inkeliz
@Exact inkeliz. Ever heard of "Postman"? You can put anything in the form and make a Submit :p
– Wallace Maxters
@Wallacemaxters if possible, also address the first option of the dropdown list, which does not have a
name
, I have to take some care of her tmb ?– MagicHat