Filters against $ _POST (or $ _GET)

Which one should you use to check if the submit button is clicked?

<input type="submit" name="action" value="Sign Up">
  • if (isset($_POST['action'] && $_POST['action'] == "Sign Up") ... or
  • if (isset(filter_input(INPUT_POST, 'action')) && filter_input(INPUT_POST, 'action') == "Sign Up")

NB: I use $ _POST ['action] ==' Sign Up 'because I use the "action" array to delete and log out, and also because some of my forms have two submit buttons, so I can distinguish )

+3
source share
6 answers

Assuming what you have <form method="post" ...>, you should use $_POST. You can check one field, for example:

if ( isset($_POST['action']) )

or any field:

if ( 0 < count($_POST) )

or check the request method:

if ( $_SERVER['REQUEST_METHOD'] === 'POST' )

or a combination above :)

, , . , , .

+4

. name, , . , , .

name='signup' name='action[signup]'.

isset($_POST['signup']) isset($_POST['action']['signup']) .

+2

, , : , , . . , , , URL- ( - ?action=signup action).

+2

, , , # 1.

. , , IP-, , , ...

$var = "192.168.2.1";
echo (int) filter_var($var, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_NO_PRIV_RANGE); // 0

. http://www.php.net/manual/en/filter.filters.validate.php . , , .

/ . . http://www.php.net/manual/en/filter.filters.sanitize.php

$var = "te)st@test.com";
echo filter_var($var, FILTER_SANITIZE_EMAIL); // "test@test.com"

filter_x , , , , , .

+1

, filter_var. isset .

0

:

if (isset($_POST['action'] && $_POST['action'] == "Sign Up") ...

isset . isset , empty.

0
source

Source: https://habr.com/ru/post/1781068/


All Articles