1

I am trying to set up a random password generator function in php but this is not working for me. I get the error:

Notice: Array to string conversion in C:\xampp\htdocs\php-testing\index.php on line 12

Array

What am I doing wrong?

<?php
function pwGenerator($len = 10) {
    $charRange = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789!@#$%^&*()";
    $pw = array();
    $length = strlen($charRange);
    for ($x = 0; $x < $len; $x++) {
        $n = rand(0, $length);
        $pw[] = $charRange[$n];
    }

    return $pw;
}

echo pwGenerator();
1
  • Well, you obviously return an array and then try to echo ... that array. For an echo it would have to be a string. So do this: return implode('', $pw); Commented May 21, 2016 at 6:57

8 Answers 8

3

You cannot convert array into string directly. So instead of storing it in array you can attach characters with a string. and now it is working.

<?php
    function pwGenerator($len = 10) {
        $charRange = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789!@#$%^&*()";
        $pw = null;
        $length = strlen($charRange);
        for ($x = 0; $x < $len; $x++) {
            $n = rand(0, $length);
            $pw .= $charRange[$n];
        }
        return $pw;
    }
    echo pwGenerator();
    ?>
Sign up to request clarification or add additional context in comments.

2 Comments

I am still getting this error though: Notice: Uninitialized string offset: 70 in C:\xampp\htdocs\php-testing\index.php on line 8
it is because random selecting 70. And your length of $charRange is 70 and it is only 69 when you put it into array. so you need to reduce 1 from $length,then it will work.
0

try this

you should define $pw as string not array

and $pw.= to concatenate the string sequence like this

<?php


function pwGenerator($len = 10) {
    $charRange = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789!@#$%^&*()";
    $pw ='';
    $length = strlen($charRange);
    for ($x = 0; $x < $len; $x++) 
    {
        $n = rand(0, $length);

        $pw.= $charRange[$n];
    }

    return $pw;
}

 echo pwGenerator();

?>

7 Comments

The additional brackets you added to the echo statement are not required, nor do they help otherwise.
Yes this works, but sometimes I am getting this error now: Notice: Uninitialized string offset: 70 in C:\xampp\htdocs\php-testing\index.php on line 12
I was just about to write that. It makes no difference? It's still an array, and arrays can't be echoed.
@Andreas No, it is indeed a string with that code. Ugly, cause slow, but working.
how you telling that its array ?
|
0

This is how I would do it. Not only is it fast, it is significantly more random (and thus secure) than some of the others, due to allowing characters to repeat and using mt_rand().

function generateRandomPassword($length = 10)
{
    static $chars = 'abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789!@#$%^&*()';
    static $charsLength = strlen($chars);

    $randomPassword = '';
    for ($i = 0; $i < $length; ++$i)
    {
         $randomPassword .= $chars[mt_rand(0, $charsLength - 1)];
    }

    return $randomPassword;
}

Declaring $chars and $charsLength as static may be overkill, but it does result in a performance improvement. Additionally, mt_rand() is more secure than rand().

The original error you received about "array to string conversion" is because you returned an array from your function. If you wanted to fix this, just change:

return $pw;

To:

return implode('', $pw);

Comments

0

A faster approach might be to shuffle a character set then grab a string of the proper length.

function random_password( $length = 10 ) {
    $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-=+;:,.?";

    $substr_length = 3;
    if( $length > $substr_length ) {

        $password = '';
        while ( strlen($password) < $length) {
            $current_length = strlen( $password );
            $substr_length = ($length - $current_length) > $substr_length ? $length - $current_length : $current_length;
            $password .= substr( str_shuffle( $chars ), 0, $substr_length );
        }

    } else {

        $password = substr( str_shuffle( $chars ), 0, $length );

    }

    return $password;
}

Borrowed from https://stackoverflow.com/a/21768419/2712737

3 Comments

Actually you first shuffle the input string, then you grab a substring. :-)
Updated verbiage to reflect that. Thanks!
This has very low entropy, as characters cannot repeat.
0

i think here your mistake is no need to declare $pw as array declare as normal string variable. try below code is working.

<?php
    function pwGenerator($len = 10) {
        $charRange = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789!@#$%^&*()";
        $pw = "";
        $length = strlen($charRange);
        for ($x = 0; $x < $len; $x++) {
            $n = rand(0, ($length-1));
            $pw .= $charRange[$n];
        }
        return $pw;
    }
    echo pwGenerator();
?>

your are return single password string value so no need to decalre as Array simple declare string variable. may it helps you.

2 Comments

If I carry out stress test on fucntion, I sometimes get: Notice: Uninitialized string offset: 70 in C:\xampp\htdocs\php-testing\index.php on line 8
@emporio try one trick ($length-1) then you will not get error if your carry out stress test on fucntion you will not get error try like that way for more reference why you need do this trick visit this link stackoverflow.com/questions/15538646/…
0

It can be an Easy Solution.

$arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T', 
'W', 'Y', '@', '#', '$', '*', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 
'd', 'e', 'f', 'g', 'h', 'm', 'n', 'p', 'q', 'r', 't', 'u', 'w', 'y', 'z');

$arr2 = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'K', 'L', 'M', 'N', 
'P', 'Q', 'R', 'T', 'W', 'Y', 'a', 'b', 'd', 'e', 'f', 'g', 'h', 'm', 
'n', 'p', 'q', 'r', 't', 'u', 'w', 'y', 'z');

        $rnd = array_random($arr, 4);
        $rnd2 = array_random($arr2, 2);

        $password = $rnd2[0].$rnd[0].$rnd[1].$rnd[2].$rnd[3].$rnd2[1];

The First and the Last character will be only Letter. Middle characters will be Letters, Numbers or Special Characters.

Comments

0

Reliable passwords You can only make from ascii characters a-zA-Z and numbers 0-9. To do that best way is using only cryptographically secure methods, like random_int() or random_bytes() from PHP7. Rest functions as base64_encode() You can use only as support functions to make reliability of string and change it to ASCII characters.

rand() is not secure and is very old. From any string You must use random_int(). From binary string You should use base64_encode() to make binary string reliable or bin2hex, but then You will cut byte only to 16 positions (values). See my implementation of this functions.

Comments

0

Rashid, that is a good starting direction.

Today, I increased the password length to seven digits, added a few, and corrected the code. I now use this code to generate a random password for a website question generator.

$arr = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'w', 'x', 'y', 'z', '@', '#', '$', '*', '!', '%', '&', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0',);

$arr2 = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'w', 'x', 'y', 'z');

        $rnd = array_rand($arr, 4);
        $rnd2 = array_rand($arr2, 3);

        $password = $arr2[$rnd2[0]].$arr[$rnd[0]].$arr[$rnd[1]].$arr2[$rnd2[1]].$arr[$rnd[2]].$arr[$rnd[3]].$arr2[$rnd2[2]];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.