Frontend\Modules\Profiles\Engine\Model::getRandomString PHP Метод

getRandomString() публичный статический Метод

Generate a random string.
public static getRandomString ( integer $length = 15, boolean $numeric = true, boolean $lowercase = true, boolean $uppercase = true, boolean $special = true ) : string
$length integer Length of random string.
$numeric boolean Use numeric characters.
$lowercase boolean Use alphanumeric lowercase characters.
$uppercase boolean Use alphanumeric uppercase characters.
$special boolean Use special characters.
Результат string
    public static function getRandomString($length = 15, $numeric = true, $lowercase = true, $uppercase = true, $special = true)
    {
        // init
        $characters = '';
        $string = '';
        $charset = FrontendModel::getContainer()->getParameter('kernel.charset');
        // possible characters
        if ($numeric) {
            $characters .= '1234567890';
        }
        if ($lowercase) {
            $characters .= 'abcdefghijklmnopqrstuvwxyz';
        }
        if ($uppercase) {
            $characters .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        }
        if ($special) {
            $characters .= '-_.:;,?!@#&=)([]{}*+%$';
        }
        // get random characters
        for ($i = 0; $i < $length; ++$i) {
            // random index
            $index = mt_rand(0, mb_strlen($characters));
            // add character to salt
            $string .= mb_substr($characters, $index, 1, $charset);
        }
        return $string;
    }

Usage Example

Пример #1
0
 /**
  * Update profile password and salt.
  *
  * @param int    $profileId Profile id for which we are changing the password.
  * @param string $password  New password.
  */
 public static function updatePassword($profileId, $password)
 {
     $profileId = (int) $profileId;
     $password = (string) $password;
     // get new salt
     $salt = FrontendProfilesModel::getRandomString();
     // encrypt password
     $encryptedPassword = FrontendProfilesModel::getEncryptedString($password, $salt);
     // update salt
     FrontendProfilesModel::setSetting($profileId, 'salt', $salt);
     // update password
     FrontendProfilesModel::update($profileId, array('password' => $encryptedPassword));
 }
All Usage Examples Of Frontend\Modules\Profiles\Engine\Model::getRandomString