命名POST / GET变量的规则? [英] Rules for naming POST/GET variables?

查看:110
本文介绍了命名POST / GET变量的规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



感谢 -

解决方案

是没有规则,我知道命名 $ _ POST $ _ GET array中的数组键。这是一个像任何其他数组。看看键盘上的这个工作示例

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

print_r($ _ POST);

对于表单输入名称,它们必须是合法的HTML名称属性(请参阅下面)。然而,在实践中,很多不寻常的角色实际上会起作用。请记住,这并不意味着这是一个好主意。不同的服务器(也可能是不同的浏览器)的行为会与空间等字符不同。

5943957 / rules-for-naming-post-get-variables / 5944013#5944013> Tadeck 已经注意到,当读取时,重复键将被最后一个键覆盖,但使用括号[] 将通过将变量转换为数组来解决此问题。



就命名约定和最佳实践而言,并不存在很多房间。建议您坚持A-Z a-z 0-9,破折号和下划线。尽管 Ajay 建议使用数据库列名称作为表单输入名称为方便起见,很多人会告诉你,向公众公开有关数据库的信息是不好的做法。我认为倒链兰可能在这里最接近这个问题的答案,并且 Tadeck 与最佳实践有最接近的答案。

关于HTMLname属性: http://www.w3.org/TR/html4/types.html#h-6.2


ID NAME 令牌必须以字母([A-Za-z])开头,后面可以跟随任意数量的字母,数字([0


,连字符( - ),下划线(_),冒号(:)和句点(。)。

也许有人可以启发我关于上述文件是否是规则或建议,我绝不是这方面的专家。在实践中,我似乎没有违反这些规则的问题。我也没有问题将这个示例文档验证为XHTML strict:

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

将其粘贴到




另一种最佳做法是添加:让你的表单输入名称或获取/发布键有意义,当然,与其他所有命名约定一样。不要使用 input1 $ _ GET ['param'] 。使用描述含义的名称,如 last_name $ _ GET ['sort_order']


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

Thanks-

解决方案

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=")(&amp;#$)%#$%" />
</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'].

这篇关于命名POST / GET变量的规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆