Jump to content

Function Return Values


shackwm60
Go to solution Solved by cyberRobot,

Recommended Posts

No doubt ive got this all wrong but i am trying to write a function that evaluates password strength/complexity before i hash/encrypt it and i want to give feedback toi the user about what is weak. I thought that i could create a variable to return a string depending on what the result is but i get nothing back. Obviously im still dont understand this too well. (yes still very noob to this). Thanks in advance for suggestion.

function password_strength($password){
        $pwdfailure = "";
    if (strlen($password) <  {
        // too short
        $pwdfailure = "Password must be at least 8 characters.";
        return $pwdfailure;
    }
    if (strlen($password) > 20) {
        // too long
        $pwdfailure = "Password cannot be longer than 20 characters.";
        return $pwdfailure;
    }
    if (!preg_match("/[A-Z]/", $password)) {
        // no upper
        $pwdfailure = "Password must contain an UPPER case character.";
       return $pwdfailure;
    }
    if (!preg_match("/[a-z]/", $password)) {
        // no lower
        $pwdfailure = "Password must contain a LOWER case character.";
       return $pwdfailure;
    }
    if (!preg_match("[^\da-zA-Z]", $password)) {
        // no lower
        $pwdfailure = "Password must contain a SPECIAL character.";
        return $pwdfailure;
    }    
    if (!preg_match("/[0-9]/", $password)) {
        // no digit
        $pwdfailure = "Password must contain at least ONE digit.";
        return $pwdfailure;
    }  
 }

Then i try to test the return value with ...

if (password_strength($confirmed_password)) {
    $pwdmismatch = $pwdfailure;   // I tried this
    $_SESSION["pwdmismatch"] = $pwdfailure;  // and this. 
       redirect_to("loginpage.php");
           } else {
   }

And i want to echo the $pwdfaioure or $_SESSION["pwdmismatch"] where the login form is. But the string is empty.

Link to comment
Share on other sites

  • Solution

$pwdfailure is no longer in scope once you leave the function. You could try something like this:

if($pwdfailure = password_strength($_GET['password'])) {
     echo $pwdfailure;   // I tried this
} else {
     echo 'Password good';
}

Also note that the following if test doesn't do what you expect:

if (!preg_match("[^\da-zA-Z]", $password)) {
     // no lower
     $pwdfailure = "Password must contain a SPECIAL character.";
     return $pwdfailure;
}
Link to comment
Share on other sites

 

$pwdfailure is no longer in scope once you leave the function. You could try something like this:

if($pwdfailure = password_strength($_GET['password'])) {
     echo $pwdfailure;   // I tried this
} else {
     echo 'Password good';
}

 

doh.. i get it. Now i understand what ginerjm was trying to tell me. Yes that worked. Thanks for that.

Edited by shackwm60
Link to comment
Share on other sites

 

$pwdfailure is no longer in scope once you leave the function. You could try something like this:

if($pwdfailure = password_strength($_GET['password'])) {
     echo $pwdfailure;   // I tried this
} else {
     echo 'Password good';
}

Also note that the following if test doesn't do what you expect:

if (!preg_match("[^\da-zA-Z]", $password)) {
     // no lower
     $pwdfailure = "Password must contain a SPECIAL character.";
     return $pwdfailure;
}

 

ok i changed the preg_match to...

if (!preg_match('/[\'\/~`\!@#\$%\^&\*\(\)_\-\+=\{\}\[\]\|;:"\<\>,\.\?\\\]/', $password)) {  
        // no SPecial Chars
        $pwdfailure = "Password must contain a SPECIAL character.";
        return $pwdfailure;
    }    

And it seems to work ive tried a couple samples.  This reg expression stuff is difficult for me to grasp. i will have to put some serious time into it when i have a chance. These examples i got from forums. thanks for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.