1

i was just make a simple script, but it don't work.

function check_length($string, $min_length, $max_length) {
  $length = strlen($string);
  if(strlen($length) < $min_length || strlen($length) > $max_length)
  { 
    header("Location: /kontakty/blad");
  }
}

check_length($_POST["name"], "3", "45");
check_length($_POST["surname"], "3", "45");
check_length($_POST["group_id"], "1", "11");

When i send data to script, the function don't check this.


Edit: I was do like You wrote and now script don't wanna redirect.

Full script

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
giertych97
  • 53
  • 1
  • 9

4 Answers4

8

You have a mistake. remove strlen on $length.

function check_length($string, $min_length, $max_length) {
      $length = strlen($string);

     if($length < $min_length || $length > $max_length)
     { 
      header("Location: /kontakty/blad");
     }
}
Zevi Sternlicht
  • 5,399
  • 19
  • 31
3

Seems you use this script on an unicode'd site (kontakty) and suppose you need to use mb_strlen instead strlen for this work. Additionally, use trim, or "a " gives 2 length in this case.

function check_length($string, $min, $max) {
    $length = mb_strlen(trim($string), 'utf-8');
    if ($length < $min || $length > $max) { 
        header('Location: /kontakty/blad');
    }
}

If mb_strlen doesn't work for you, can use this:

function strlen_unicode($str) {
    return count(preg_split(
        '~~u', $str, -1, PREG_SPLIT_NO_EMPTY));
}
Kerem
  • 11,377
  • 5
  • 59
  • 58
  • do you know why my mb_strlen didn't working? i try in my local server and it's working but when I upload mb_strlen and strlen length is same. Please reply if you can. Really appreciate. – Scramble Aug 18 '15 at 09:11
  • @scramble; you should set internal encoding using `mb_internal_encoding` func at the first. – Kerem Aug 20 '15 at 11:51
  • Then why in my local server mb_strlen working well but in my hosting it doesn't work well? doesn't it because configuration in my local server? – Scramble Aug 24 '15 at 09:03
2
function check_length($string, $min_length, $max_length) {

  $length = strlen($string);  <= calculated once

  if(strlen($length) < $min_length || strlen($length) > $max_length)
  ^^^^^^^^^^^^^                       ^^^^^^^
                                     no need of this it will calculate length of 
                                     $length no string   

  { 
    header("Location: /kontakty/blad");
  }
}

try

function check_length($string, $min_length, $max_length) {

  if(strlen($string) < $min_length || strlen($string) > $max_length)
  { 
    header("Location: /kontakty/blad");
  }
}

according to the code in your comment link


Note

  1. The entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_, is officially deprecated as of PHP v5.5.0 and will be removed in the future. So use either PDO or MySQLi

Good read

  1. The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead
  2. PDO Tutorial for MySQL Developers
  3. Pdo Tutorial For Beginners
Community
  • 1
  • 1
NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
1

Replace strlen($length) with $length in the line:

if(strlen($length) < $min_length || strlen($length) > $max_length)

You are taking the length of the length which make no sense.

NullPoiиteя
  • 56,591
  • 22
  • 125
  • 143
MTilsted
  • 5,425
  • 9
  • 44
  • 76