php - Avoid requests that take much time Apache -


i programm class validates data browsers , 1 of methods validates length of strings, came mind: if sends big string 2 millions of characters or more (or whatever) ?

if use strlen() count bytes, count last bytes. waste count bytes.

after thinking while, made this:

   class validator     {      static public function verify_str_length($str, $min, $max)      {           $i;        $counter = $min;        $msg = "";       // looling until null char found       //        for($i=$min-1;$i<$max;$i++) {           if(!isset($str[$i])) {             if($i == ($min -1)) {                 // if first iteration                 // find null char early.                 // $i starts minimum length allowed, string                 // length lower short                 $msg = 'too short string';                 return -1;             }              return 0;          }        }        if(isset($str[$i])) {          // if reach max , keep without finding null char          // string length higher $max           $msg = 'too long string';            return 1;       }        return 0;        }       //     /*  others methods           ..... */    } 

note not need number of characters in string, if higher $min , lower $max. discard others chars.

my question is: idea instead of using strlen() ?

is there way configurate apache stop execution if server takes more x seconds in processing request ?

or can use both options?

thanks in advance!

you can use php's post_max_size directive limit amount of content submitted. careful setting because if have file uploads, have fit within size well.

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

to limit amount of time spent parsing input data, may use max_input_time.

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

to limit execution time, use max_execution_time.

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

you may set these in .htaccess, so:

php_value post_max_size 1m php_value max_execution_time 30 php_value max_input_time 5 

for validation, should use php's filter functions, example:

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

this ensure if $_post['content'] not composed of letters, digits, underscores or hyphens, , not between 1 , 64 characters long, not accepted.

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


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -