简体   繁体   English

过滤器vs $ _POST(或$ _GET)

[英]Filters vs $_POST (or $_GET)

Which one are you supposed to use, to check if a form submit button has been clicked? 您应该使用哪一个,检查是否已单击表单提交按钮?

<input type="submit" name="action" value="Sign Up">
  1. if (isset($_POST['action'] && $_POST['action'] == "Sign Up") ... or if (isset($_POST['action'] && $_POST['action'] == "Sign Up") ...或者
  2. 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 for delete and logout as well, and because some of my forms have two submit buttons, so I can differentiate). 注意:我使用$ _POST ['action] =='注册',因为我使用'action'数组进行删除和注销,因为我的一些表单有两个提交按钮,所以我可以区分)。

Assuming you have <form method="post" ...> , you should use $_POST . 假设你有<form method="post" ...> ,你应该使用$_POST You can check for a single field, for example: 您可以检查单个字段,例如:

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

or any field: 或任何领域:

if ( 0 < count($_POST) )

or check request method: 或检查请求方法:

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

or a combination of above :) 或以上的组合:)

I don't recommend checking submit button's value, if it changes, you need to modify the code as well. 我不建议检查提交按钮的值,如果更改,您还需要修改代码。 Not to mention what will you need to do if the site is or becomes multi lingual. 更不用说如果网站是多语言,你需要做什么。

Not actually answering the question. 实际上没有回答这个问题。 But I would give the different submit-buttons different name 's, so they are not bound to the value which is displayed on the button. 但是我会给不同的提交按钮指定不同的name ,因此它们不受按钮上显示的值的约束。 Which in turn makes it less error prone to change the text on the button. 这反过来使得更改按钮上的文本更不容易出错。

For instance name='signup' or name='action[signup]' . 例如name='signup'name='action[signup]'

And check with isset($_POST['signup']) or isset($_POST['action']['signup']) respectively. 并分别检查isset($_POST['signup'])isset($_POST['action']['signup'])

I'm not sure, but I think I bumped into this problem once: Forms can be posted by pressing enter, in which case no value for the button is posted. 我不确定,但我想我曾经遇到过这个问题:按Enter键可以发布表格,在这种情况下,没有发布按钮的值。 So I would strongly discourage to use three different submit buttons for three such different actions. 因此,我强烈反对使用三个不同的提交按钮进行三种不同的操作。 Instead, I'd use three forms, and use another way to distinguish between them, like putting a hidden field in them or posting to a different url (or put something like ?action=signup in the forms action attribute. 相反,我会使用三种形式,并使用另一种方式来区分它们,比如在其中放置隐藏字段或发布到不同的URL(或在表单action属性中添加类似?action=signup的内容)。

If it's just the exact value of an input you want while expecting a very specific value, just use #1. 如果它只是想要非常具体的值时所需输入的确切值,那么只需使用#1即可。

Using filters can save the amount of code needed. 使用过滤器可以节省所需的代码量。 As opposed to writing out all the code needed to check if an IP address you're given is, say not on a private range, you can reduce the code to just this... 而不是写出检查你所提供的IP地址所需的所有代码,而不是私有范围,你可以将代码减少到这个......

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

See http://www.php.net/manual/en/filter.filters.validate.php for additional filter types and option flags. 有关其他过滤器类型和选项标志,请参见http://www.php.net/manual/en/filter.filters.validate.php Additionally, you can pass the value to a callback function to customize how you filter data. 此外,您可以将值传递给回调函数以自定义过滤数据的方式。

You can also change/sanitize the input with less code. 您还可以使用更少的代码更改/清理输入。 See http://www.php.net/manual/en/filter.filters.sanitize.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"

Though using filter_x functions allows for writing out less code, I personally have no idea how it performs compared to being handled by actual verbose code nor do I personally know exactly how reliable the filters and sanitizers are. 虽然使用filter_x函数允许写出更少的代码,但我个人不知道它与实际详细代码处理相比它的表现如何,我个人也不知道过滤器和消毒剂究竟有多可靠。

For just checking if it's there, you don't need filter_var . 只是检查它是否在那里,你不需要filter_var isset alone is sufficient. isset本身就足以。

The first one is the right one: 第一个是正确的:

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

Because using a function in isset will trigger a parse error. 因为在isset使用函数会触发解析错误。 Only variables can be used with isset and other language constructs like empty . 只有变量可以与isset和其他语言结构一起使用,例如empty

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

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