简体   繁体   English

php内置过滤器

[英]php built-in filter

I want to make a little contact form, where the user inputs his name, email, and the message. 我想制作一个小联系表单,用户输入他的姓名,电子邮件和消息。 The form is submitted with ajax to a php file that has to do the following: 表单与ajax一起提交到php文件,该文件必须执行以下操作:

1- check if the 3 posted variables exist and not NULL 2- sanitize and verify the 3 variables for malicious code and some criteria, like the name must be and the email must be.. 3- send the data with php's mail(). 1-检查3个发布的变量是否存在而不是NULL 2-清理并验证3个变量是否有恶意代码和一些标准,如名称必须和电子邮件必须是... 3-用php的mail()发送数据。

how can I achieve the first and the second steps with php filter 如何用php过滤器实现第一步和第二步

NB: I took a look at the php's manual and I understood nothing. NB:我看了一下php的手册,我什么都不懂。

Thanks. 谢谢。

1. Use isset() or array_key_exists() on $_POST to see if values exist. 1.在$ _POST上使用isset()array_key_exists()来查看值是否存在。

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

// or

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

You can also use filter_has_var, but it's also got a "gotcha" that you need to be aware of. 你也可以使用filter_has_var,但它也有一个你需要注意的“问题”。 It does not work off of PHP's superglobals, instead relying on the data that's sent to PHP. 它不适用于PHP的超全局,而是依赖于发送给PHP的数据。 If you manually declare something in your script, eg $_POST['test'] , filter_has_var will not see it. 如果您在脚本中手动声明某些内容,例如$_POST['test'] ,filter_has_var将无法看到它。

How to use filter_has_var: 如何使用filter_has_var:

if (filter_has_var(INPUT_POST, 'test'))



2. Do you want to sanitize data or validate it? 2.您想要清理数据还是验证数据? (two different things). (两件事不同)。

Assert that name and email have values, and that email is a valid email: 断言姓名和电子邮件具有值,并且该电子邮件是有效的电子邮件:

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

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

you can do this for email: 你可以这样做的电子邮件:

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

Returns the filtered data, or FALSE if the filter fails. 返回已过滤的数据,如果过滤器失败,则返回FALSE。

you can for just validate the value do this with array. 你可以只使用数组验证值。


$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 multi filter or multi flag to one field like this 你可以像这样在一个字段中添加多个过滤器或多个标志

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM