duangua6912 2017-08-18 01:07
浏览 93
已采纳

避免需要花费很多时间的Apache

I was about to programm a class that validates data from browsers and one of those methods validates length of strings, then something came to my mind: What if somebody sends a very big string with 2 millions of characters or more (or whatever) ?

If I use strlen() to count bytes, it will count to the last bytes. It would be a waste to count all those bytes.

After thinking for a while, I made something like this:

   Class Validator
    {
     static public function verify_str_length($str, $min, $max)
     {   
       $i;
       $counter = $min;
       $msg = "";
      // looling until null char is found
      //
       for($i=$min-1;$i<$max;$i++) {
          if(!isset($str[$i])) {
            if($i == ($min -1)) {
                // if first iteration
                // we find the null char so early.
                // $i starts with the minimum length allowed, the string
                // length is lower than that so it is too short
                $msg = 'Too short string';
                return -1;
            }
             return 0;
         }

      }
       if(isset($str[$i])) {
         // if we reach the max and keep without finding the null char so
         // the string length is higher than $max
          $msg = 'Too long string';
           return 1;
      }
       return 0;
       }
      //
    /*  Others Methods 
         ..... */
   }

Note that I do not need the number of characters in the string, only if it higher than $min and lower than $max. I will discard all others chars.

My question is: would it be a good idea to do so instead of using strlen() ?

Is there another way to do this like configurate APACHE to stop execution if the server takes more than X seconds in processing the request ?

Or can I use both options?

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • dongtai6741 2017-08-18 01:38
    关注

    You can use PHP's post_max_size directive to limit the amount of content submitted. Be careful with this setting because if you have file uploads, they will have to fit within this size as well.

    http://php.net/manual/en/ini.core.php#ini.post-max-size

    To limit the amount of time spent parsing input data, you may use max_input_time.

    http://php.net/manual/en/info.configuration.php#ini.max-input-time

    To limit the execution time, use max_execution_time.

    http://php.net/manual/en/info.configuration.php#ini.max-execution-time

    You may set these in .htaccess, like so:

    php_value post_max_size 1M
    php_value max_execution_time 30
    php_value max_input_time 5
    

    For validation, you should use PHP's filter functions, for example:

    $content = filter_input( INPUT_POST, 'content', FILTER_VALIDATE_REGEXP, [ 'options' => ['regexp' => '/^[\w-]{1,64}$/']] );
    

    This will ensure that if $_POST['content'] is not composed of letters, digits, underscores or hyphens, and is not between 1 and 64 characters long, it will not be accepted.

    http://php.net/manual/en/function.filter-input.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?