私はブラウザからのデータを検証するクラスを作成しようとしていましたが、それらのメソッドの1つが文字列の長さを検証すると、何かが私の頭に浮かんできました:誰かが2百万文字以上のもの(何でも)?多くの時間を要するリクエストを避けるApache
バイト数をカウントするためにstrlen()を使用すると、最後のバイト数がカウントされます。 これらすべてのバイトを数えるのは無駄でしょう。
しばらく考えた後、私はこのような何か作っ:私はそれが$ minよりも高いと$ maxよりも小さい場合には、文字列の文字数を必要としない
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
..... */
}
注意を。私は他のすべての文字を破棄します。
私の質問です:strlen()を使用する代わりにそうすることをお勧めしますか?
サーバーが要求の処理にX秒以上かかる場合、実行を停止するようにconfigurate APACHEのようにこれを行う別の方法はありますか?
どちらのオプションも使用できますか?
ありがとうございます!
いいえstrlenを使用してください。 – user2182349
応答いただきありがとうございます。不要なバイトを数えないようにするにはどうすればよいですか? –