duanbiyi7319 2014-04-02 03:21 采纳率: 100%
浏览 23
已采纳

我想将文本的最大用户输入设置为600字节。 PHP

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>
  • 写回答

2条回答 默认 最新

  • drgc9632 2014-04-02 03:35
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?