Hey guys. I'm trying to make a script to register users. At the moment I'm trying to create some code that will determine if the password submitted is at least 6 characters. Now it works if the password only contains letters. However, if I use integers, the code doesn't work. I'm using strlen to determine length, which I know won't work with integers. Is there a way to convert ints to strings? here is my code

	if (strlen($_POST['pass'] < 6) ) 
	{
	die("Sorry, your password has to be at least 6 characters. <a href='registration.php>Please choose another</a>'");
	}
    if (strlen(strval($_POST['pass'])) < 6) ) 
    	{
    	die("Sorry, your password has to be at least 6 characters. <a href='registration.php>Please choose another</a>'");
    	}

      Thanks stasonis for your reply.

      However, theres still a problem. strval will work if the password is mixed with letters and numbers ie. (a1b2c3). It wont work if the password is just numbers.

      -Brandon

        You sure about that? As a matter of fact strlen should automatically do the conversion for you anyways

            $x = 123;
            echo strlen ($x);

        This outpus 3.

          Even if someone enters an integer as a string as in $number= "132072"; and you echo strlen($number); it will return 6. PHP is funny though and if you did something like

          <?php
          $anumber="20";
          $aninteger = 25;
          echo $anumber + $aninteger; //Prints 45 becuase PHP sees that the $anumber is an integer
          ?>
            Stasonis wrote:

            You sure about that? As a matter of fact strlen should automatically do the conversion for you anyways

                $x = 123;
                echo strlen ($x);

            This outpus 3.

            Your right. I got it to work by adding a variable. I don't think that it will work if I use the $Post. I have to create a variable that is equal to the $POST ... then strlen works.

            if ($_POST['pass']) {
            	$value = $_POST['pass'];
            
            if (strlen($value) < 6) 
            {
            die("Sorry, your password has to be at least 6 characters. <a href='registration.php'>Please choose another</a>");
            }
            
            }
            

              the right way to check a value that has been sens via $_POST it to stripslashes. Some chars are scaped when submitting into a form

              it's pretty weird that strlen freaks out when processing the var... it should see it normally, since data transmissted via $_POST are aways strings, you can also try to force strlen to read the var as a string

              	if (strlen(stripslashes((string) $_POST['pass'])) < 6) 
              

                The reason for all this is in the manual

                The type of a variable is usually not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which that variable is used.

                Silent type conversion can kill you in both mysql and php.

                Personally, I'd not allow an all numbers password, but that is just me.

                  I'm suprised your original code worked at all considering the wrong placement of the closing paren on strlen.

                  if (strlen($_POST['pass'] < 6) )

                  Instead of assigning a new vaiable, you could cast it to a string variable.

                  if(strlen((string)$_POST['pass']) < 6)

                    Write a Reply...