Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

For some reason I can not get my function to return a string...

$password = crypt_password_input($password, "");

//Encrypt Password longer than 8 characters
function crypt_password_input($inputPassword, $newPassword)
{
    $passwordLength = strlen($inputPassword);

    if($passwordLength > 8){
        $encryptString = substr($inputPassword, 0, 8);
        $inputPassword = substr($inputPassword, 8);
        $newPassword .= crypt($encryptString, "HIDDENSALT");
        crypt_password_input($inputPassword, $newPassword);
    }else{
        $newPassword .= crypt($inputPassword, "HIDDENSALT");
        echo "Final: " . $newPassword . "<br/>";
        return $newPassword;
    }
}


echo "Encrypted from the input: " . $password . "<br/>";

This is the output of this script...

Final: ltu1GUwy71wHkltVbYX1aNLfLYltEZ7Ww8GghfM
Encrypted from the input:

share|improve this question
    
acutally you return the string which is ltu1GUwy71wHkltVbYX1aNLfLYltEZ7Ww8GghfM –  Robert Aug 5 '13 at 7:49
add comment

2 Answers

up vote 1 down vote accepted

you have no return statement under this condition block. i have added return there.

if($passwordLength > 8)
{
    $encryptString = substr($inputPassword, 0, 8);
    $inputPassword = substr($inputPassword, 8);
    $newPassword .= crypt($encryptString, "HIDDENSALT");
    return crypt_password_input($inputPassword, $newPassword);
}
share|improve this answer
add comment

I am not sure about your logic, but your code should be like this:

$password = crypt_password_input($password, "");

//Encrypt Password longer than 8 characters
function crypt_password_input($inputPassword, $newPassword)
{
    $passwordLength = strlen($inputPassword);

    if($passwordLength > 8)
    {
        $encryptString = substr($inputPassword, 0, 8);
        $inputPassword = substr($inputPassword, 8);
        $newPassword .= crypt($encryptString, "HIDDENSALT");
        return crypt_password_input($inputPassword, $newPassword);
    }
    else
    {
        $newPassword .= crypt($inputPassword, "HIDDENSALT");
        echo "Final: " . $newPassword . "<br/>";
        return $newPassword;
    }
}


echo "Encrypted from the input: " . $password . "<br/>";

In your code, you are recursively calling the input but not returning anything, so it fails if you have password longer than 8 characters.

share|improve this answer
    
He doesn't want it to return until the password has reached a length of 8 or more characters. –  Jared Aug 5 '13 at 7:51
    
Yeah I did have $password = crypt_password_input($password, ""); for some reason I didn't write it here lol. I do see that I needed the return on the true condition. Thanks Guys :D –  Tom Hanson Aug 5 '13 at 7:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.