POST/GET 变量命名规则?

发布于 2024-11-06 19:09:50 字数 62 浏览 0 评论 0原文

在命名表单中的 POST 变量或查询字符串中的 GET 变量时,是否需要遵循任何规则?

谢谢-

Are there any rules one needs to follow when naming POST variables in a form or GET variables in a query string?

Thanks-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情深缘浅 2024-11-13 19:09:50

从字面上回答这个问题,据我所知,在 php 中命名 $_POST$_GET 数组键确实没有“规则”。它是一个像其他数组一样的数组。看看Codepad 上的这个工作示例

<?php
$_POST['♠♣♥♦'] = 'value1';
$_POST['\'\'\'\''] = 'value2';
$_POST['<?php echo "Hello World"; ?>'] = 'value3';
$_POST['     '] = 'value4';
$_POST[''] = 'value5';
$_POST['@#$%^&*()'] = 'value6';

print_r($_POST);

对于表单输入名称,它们必须是合法的HTML“名称”属性(见下文)。然而,在实践中,许多不寻常的字符实际上会起作用。请记住,这并不意味着这是一个好主意。不同的服务器(可能还有不同的浏览器)对于某些字符(例如空格)的行为会有所不同。

正如 Tadeck 所指出的,重复的键将被覆盖阅读时的最后一个,但使用括号[]将在客户端通过将变量转换为数组来解决这个问题。

至于命名约定和最佳实践,没有太多空间。建议您坚持使用 AZ az 0-9、破折号和下划线。尽管 Ajay 建议使用数据库列名称作为表单输入名称为了方便起见,许多人会告诉您,向公众公开有关数据库的信息是不好的做法。我认为 invertedlambda 可能对这个问题有最接近的答案, 和 Tadeck 就最佳实践而言有最接近的答案。

关于 HTML“名称”属性: http://www.w3.org /TR/html4/types.html#h-6.2

IDNAME 令牌必须以字母 ([A-Za-z]) 开头,后面可以跟任意数量的字母、数字 ([0- 9])、连字符(“-”)、下划线(“_”)、冒号(“:”)和句点(“.”)。

也许有人可以启发我,上述文件是规则还是建议,我绝不是这方面的专家。在实践中,我似乎对违反其中一些规则没有任何问题。我也可以将此示例文档验证为严格的 XHTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
</head>
<body>
<div><form action="" method="post">
<div>
<input name="♠♣♥♦" />
<input name="''''" />
<input name=")(&#$)%#$%" />
</div>
</form>
</div>
</body>
</html>

将其粘贴到 验证器 中,它将经过。


要添加的另一个最佳实践是:使您的表单输入名称或 get/post 键有意义,当然就像所有其他命名约定一样。不要使用 input1$_GET['param']。使用描述含义的名称,例如 last_name$_GET['sort_order']

TO answer the question literally, there really are no "rules" I'm aware of for naming $_POST and $_GET array keys in php. It's an array like any other. Take a look at this working example on Codepad:

<?php
$_POST['♠♣♥♦'] = 'value1';
$_POST['\'\'\'\''] = 'value2';
$_POST['<?php echo "Hello World"; ?>'] = 'value3';
$_POST['     '] = 'value4';
$_POST[''] = 'value5';
$_POST['@#$%^&*()'] = 'value6';

print_r($_POST);

In the case of form input names, they just have to be legal HTML "name" attributes (see below). However, in practice, a lot of unusual characters will actually work. Keep in mind that this doesn't mean it's a good idea. Different servers (and probably different browsers) will act differently with some characters like spaces for instance.

As Tadeck has noted, duplicate keys will be overwritten by the last one when reading, but using brackets[] will solve this on the client side by turning the variable into an array.

As far as naming conventions and best practices, there isn't a lot of room. It's suggested that you stick to A-Z a-z 0-9, dashes, and underscores. Although Ajay has suggested using database column names for form input names as a matter of convenience, many people will tell you that it is bad practice to expose information about your database to the public. I think invertedlambda probably has the closest answer here to the question, and Tadeck has the closest answer as far as best practices.

Regarding HTML "name" attributes: http://www.w3.org/TR/html4/types.html#h-6.2

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

Maybe someone can enlighten me as to whether or not the above document is a rule or a recommendation, I'm by no means an expert on this subject. I seem to have no issues breaking some of these rules in practice. I also have no problem validating this example document as XHTML strict:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title></title>
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8" />
</head>
<body>
<div><form action="" method="post">
<div>
<input name="♠♣♥♦" />
<input name="''''" />
<input name=")(&#$)%#$%" />
</div>
</form>
</div>
</body>
</html>

Paste it into the validator, it will pass.


One more best practice to add: Make your form input names or get/post keys meaningful, as with every other naming convention of course. Don't use input1 and $_GET['param']. Use names that describe the meaning, like last_name or $_GET['sort_order'].

潇烟暮雨 2024-11-13 19:09:50

我相信最好的解决方案是:

  • 使用小写,
  • 不使用点,任何其他特殊字符(下划线是可以接受的),
  • 了解它们在请求期间传递的方式(例如 name="test[][]"< /code> 将在其他数组内的数组中创建值)并正确使用它,
  • 避免产生冲突(例如 ?test=1&test=2 将产生问题,因为只有一个值会通过-更好地利用?test[]=1&test[]=2 因此将传递具有两个值的数组),
  • 保持一致,

此外,浏览您可能在 GitHub.com 因此您将使用经过许多人测试和使用的良好实践。

I believe the best solution is to:

  • use lower cases,
  • NOT use dots, any other special characters (undescores are acceptable),
  • understand the way they are passed during request (eg. name="test[][]" will create value within array that is within other array) and use it properly,
  • avoid creating conflicts (eg. ?test=1&test=2 will create problems as only one of the values will be passed - better use ?test[]=1&test[]=2 so the array with two values will be passed),
  • be consistent,

Furthermore, browse through different solutions that you may find on GitHub.com so you will be using practices that are good, tested and used by many people.

我一直都在从未离去 2024-11-13 19:09:50

简而言之,不,只要您的变量名称符合 HTTP 规范和您正在使用的支持 Web 服务器,您就可以根据需要调用任何参数。

如果您使用与命名变量时相同的指导原则来命名参数,那么您应该能够选择一个好的名称。

In a word, no -- as long as your variable names are compliant with the HTTP specification and the backing web server that you're using, you can call your parameters anything you want.

If you use the same guidelines for naming a parameter as you use when you name variables, you should be able to pick a good name.

墨小沫ゞ 2024-11-13 19:09:50

我们可以像变量名称规则一样使用名称,但也可以使用关键字作为 GET/POST 变量名称。
更好的是,我们可以在适用的情况下使用与数据库列名称相同的名称。但没有这样的规则。
这些只是一些建议。

We can use the names as like variable names rules , but can use the keywords also as GET/POST Variable names.
Better we can use the same names as database column names where ever applicable. But there is no rule like this.
Just these are some recommendations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文