Search code examples
phpformsstrlen

PHP: strlen() not functioning...At all


I've tried looking at other answers, but all I come across is people placing their comparison operators in the wrong direction. Not the case here. Below is a simplified version of my code. This code does not return the error I believe it SHOULD.

I'm trying to determine if a string, in this case a password, meets a minimum length requirement. However, it never gives the error it should, regardless of the string I put in. As a means of debugging, I tried inputting a simple string, "hi" into strlen() and told it to give an error if it was less than 6 bytes long. However, even that wouldn't return as I wanted it to.

I feel like I'm missing something silly. Help?

<?php
$Email = mysql_real_escape_string(Trim($_POST['Email']));
$Password = "hi";

// check fields
$error = false;
if(empty($Email)) $error = true;
if(strlen(utf8_decode($Password)) < 6) $error = true;

if($error != false) header("Location: index.php?error=true");


// store user to database
$success = insert_dbUser(new User($Email, $Password));

// redirect to success page 
if ($success) {
    header("Location: index.php?success=true");
}
else header("Location: index.php?error=existinguser");
?>

Solution

  • Make sure to end the script after sending location headers:

    if ($error) {
        header("Location: index.php?error=true");
        exit();
    }