Built-in php filter

I want to create a small contact form in which the user enters his name, email address and message. The form is submitted using ajax to the php file, which should do the following:

1- check if 3 published variables exist, and not NULL 2 — sanitize and check 3 variables for malicious code and some criteria, for example, the name should be and the message should be .. 3 - send data using php mail ().

how can i complete the first and second steps with php filter

NB: I took a look at the php manual and I didn’t understand anything.

Thank.

+3
source share
2 answers

1. Use isset () or array_key_exists () in $ _POST to see if values ​​exist.

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

// or

if (array_key_exists('a_field', $_POST))

filter_has_var, "gotcha", . PHP, , PHP. - script, . $_POST['test'], filter_has_var .

filter_has_var:

if (filter_has_var(INPUT_POST, 'test'))



2. ? ( ).

, , :

if (!empty($_POST['name']))

if (!empty($_POST['email']) && filter_input(INPUST_POST, 'email', FILTER_VALIDATE_EMAIL))
+3

:

filter_var('name@domain.com', FILTER_VALIDATE_EMAIL)

FALSE, .

.


$args = array(
    'name'   => array('filter'    => FILTER_VALIDATE_BOOLEAN,
                            'flags'     => FILTER_NULL_ON_FAILURE),
    'email'    => array('filter'    => FILTER_VALIDATE_BOOLEAN,
                            'flags'     => FILTER_NULL_ON_FAILURE),
     'message'    => array('filter'    => FILTER_VALIDATE_BOOLEAN,
                            'flags'     => FILTER_NULL_ON_FAILURE));

$myinputs = filter_input_array(INPUT_GET, $args);

you can add several filters or multiflag in one field, for example

email => array("filter" => array(FILTER_VALIDATE_EMAIL ,FILTER_VALIDATE_BOOLE)

+1
source

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


All Articles