我想将文本的最大用户输入设置为600字节。(I want to set the maximum user input of the text to 600 bytes . PHP)

编程入门 行业动态 更新时间:2024-10-27 20:32:22
我想将文本的最大用户输入设置为600字节。(I want to set the maximum user input of the text to 600 bytes . PHP)

我别无选择,只能请各位专业人士帮助我。

好吧,我有一个HTML表单,允许用户输入消息和名称。我想将消息的最大字节设置为600字节,并将名称的最大字节设置为30字节,但我应该使用PHP来设置限制用户输入? 如果有办法在PHP中处理它我想知道。

<form id="formed" action="second.php" method="post"> <textarea rows="5" cols="20" name="text" id="text"></textarea><br /> <input type="text" name="name" id="name"/><br /> <input type="submit" name="sub" id="sub"/><input type="button" id="display" value="display"> </form>

I had no option but to ask you pros to help me out.

Well,I have a HTML form that allows the user input a message and a name.I wanted to set the message's maximum byte to 600-byte and set the name's max byte to 30 byte, but should I use the php to set the limit of user input? If there is a way to handle it in php I would want to know .

<form id="formed" action="second.php" method="post"> <textarea rows="5" cols="20" name="text" id="text"></textarea><br /> <input type="text" name="name" id="name"/><br /> <input type="submit" name="sub" id="sub"/><input type="button" id="display" value="display"> </form>

最满意答案

您应该在客户端和服务器端都检测到这一点。 在客户端执行此操作可让用户知道他们不应再生成字符串,并且在服务器端执行此操作可确保字符串实际上不会超出您的需要。

任何人都可以通过浏览器中的开发人员工具修改客户端上的html,这样就可以删除html元素的maxlength属性,然后发送长度超过30个字符的字符串。

<textarea rows="5" cols="20" name="text" id="text" maxlength="600"></textarea> <input type="text" name="name" id="name" maxlength="30"> if(strlen($_POST['name']) > 30) { // Name is too long, report an error to the user } if(strlen($_POST['text']) > 600) { // Text is too long, report an error to the user }

You should detect this on both the client and server side. Doing it on the client side lets the user know they shouldn't make the string any longer, and doing it on the server side ensures that the string actually won't be any longer than what you want.

The html on the client side can be modified by anyone through a tool such as the developer tools in a browser, so one could remove the maxlength property of your html elements and then send a string much longer than 30 characters.

<textarea rows="5" cols="20" name="text" id="text" maxlength="600"></textarea> <input type="text" name="name" id="name" maxlength="30"> if(strlen($_POST['name']) > 30) { // Name is too long, report an error to the user } if(strlen($_POST['text']) > 600) { // Text is too long, report an error to the user }

更多推荐

本文发布于:2023-07-31 08:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1342572.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:设置为   字节   想将   文本   用户

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!