0

我想在我的项目中使用Randomness组件,但无法配置它。当我在做

$ss = Yii::app()->Randomness::randomString(32);

这在 actionIndex() 它给我一个错误

Parse error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM in /protected/controllers/SiteController.php on line 32

我把它放在我的 main.php 配置文件中

'components'=>array(
      'Randomness'=>array('class'=>'Randomness'),
      [......]
),

所以,你能帮帮我吗?

-------------------------------------------------- ---------已编辑---------------------------------------- --------------------------

感谢您的所有回答,但 Randomness 似乎不适用于 1and1.com 托管。但好消息是我发现了另一种生成随机字符串的好方法。我正在使用在 1and1.com 上似乎有效的“/dev/urandom”。这是代码:

<?php
class Random extends CApplicationComponent
{
    public static function intRandom($min, $max) 
    {
        $bits = '';

        $diff = $max-$min;
        $bytes = ceil($diff/256);


        $fp = @fopen('/dev/urandom','rb');
        if ($fp !== FALSE) {
            $bits .= @fread($fp,$bytes);
            @fclose($fp);
        }
        $bitlength = strlen($bits);
        for ($i = 0; $i < $bitlength; $i++) {
            $int =  1+(ord($bits[$i]) % (($max-$min)+1));
        }
        return $int;
    }

    public static function strRandom($length) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+=|]}[{;:?.,></"; 

        $size = strlen( $chars );
        for( $i = 0; $i < $length; $i++ ) {
            $str .= $chars[ self::intRandom(0, strlen($chars)-1) ];
        }

        return $str;
    }
}

但是现在我有另一个问题,由于 $char 中使用的字符,我什么时候需要保存到数据库中的盐,我会有一些问题吗?

4

2 回答 2

1

为什么不只是Randomness::randomString(32);?(由文档指定)

于 2012-02-24T11:03:04.673 回答
0
Yii::app()->Randomness->randomString = 32;

确保您的类Randomness具有属性randomString

于 2012-02-24T11:23:46.260 回答