使用maxlength标签通过PHP检查表单输入长度

And*_*s7X 0 php forms input maxlength

我的问题很简单:如果我在HTML表单中有一些带有maxlength标签的输入字段,我还必须通过PHP检查输入长度吗?

Ofi*_*uch 8

你应该,因为任何人都可以"复制"你的表格并使用它.服务器端检查优于客户端.(它是安全的)

例如,您的表格:

<form method='post' action='/pay.php'>
<input type='text' maxlength='10' name='addres'>
</form>
Run Code Online (Sandbox Code Playgroud)

我可以轻松地做这样的事情:(并将其上传到一些免费的服务器或类似的东西)

<form method='post' action='http://www.yourdomain.com/pay.php'>
<input type='text' name='addres'><!--no maxlength='10' for me-->
</form>
Run Code Online (Sandbox Code Playgroud)

现在我有点"超越"你的限制.

但是,您可以编辑您的pay.php文件:

$addres = clean_input($_POST['addres']); //A custom function to make sure it's a clean string
if(strlen($addres) > 100)
{
  //Something weird
}
else
{
  //We're good
}
Run Code Online (Sandbox Code Playgroud)