注意:未初始化的字符串偏移量:在第316行的/.../libraries/functions.php中为62

时间:2012-04-24 09:40:47

标签: php

男人哦,我无法弄明白......请帮助。 这里缺少什么?

这里是错误:

Notice: Uninitialized string offset: 62 in /..../libraries/functions.php on line 316
Notice: Undefined variable: stilldo in /..../libraries/functions.php on line 322

以下是代码:

function generate_password($length = 12, $letters = true, $mixed_case = true, $numbers = true, $punctuation = false){

    ## Generate the alfa
    $alfa = '';
    if($letters == true)    $alfa .= 'abcdefghijklmnopqrstuvwxyz';
    if($mixed_case == true) $alfa .= strtoupper('abcdefghijklmnopqrstuvwxyz');
    if($numbers == true)    $alfa .= '0123456789';
    if($punctuation == true)$alfa .= '~!@#$%^&*(),.=+<>';

    ## Set Random Seed
    if(!function_exists('_generate_password_seed')):
    function _generate_password_seed(){
        list($usec, $sec) = explode(' ', microtime());
        return (float) $sec + ((float) $usec * 100000);
    }
    endif;
    srand(_generate_password_seed());

    ## Generate the password
    $token = "";
    for($i = 0; $i < $length; $i ++) {
        $token .= $alfa[@rand(0, @strlen($alfa))];
    }

    ## Check the length
    if(strlen($token) < $length){
        // echo $stilldo = $length - strlen($token);
        $token .= generate_password($stilldo);
    }

    ## Return the password
    return $token;
}

我在运行此功能的10次中得到此错误,我似乎无法破解它。

1 个答案:

答案 0 :(得分:0)

Notice: Undefined variable: stilldo表示变量$stilldo未定义,也未初始化......这意味着,在向其传递未定义变量时,在递归中调用函数generate_password,即事实具有空值。因此,您的功能无法正常工作。

您的代码部分应如下所示:

## Check the length
if(strlen($token) < $length){
    $stilldo = $length - strlen($token);
    $token .= generate_password($stilldo);
}

是的,一个愚蠢的错误 - 你注释掉了正确的行(只有echo应该被注释掉或删除)。

享受!