gpt4 book ai didi

php - str_random 在 laravel 中是唯一的吗?

转载 作者:行者123 更新时间:2023-12-01 22:34:19 25 4
gpt4 key购买 nike

我使用 str_random(60) 函数生成密码重置代码。我的问题是,如果成千上万的人要求重置密码,这个代码是唯一的还是可以重复?

public function postForgotPassword(){

$validator = Validator::make(Input::all(), array('email'=>'required|email'));

if($validator->fails()){

return Redirect::route('account-forgot-password')->withErrors($validator)->withInput();
}else{

$user= User::where('email', '=', Input::get('email'));

if($user->count()){

$user = $user->first();

$code = str_random(60);
$password = str_random(10);

$user->code = $code;
$user->password_temp = Hash::make($password);

if($user->save()){

Mail::send('emails.auth.forgot', array('link'=>URL::route('account-recover', $code), 'username'=>$user->username,'password'=>$password), function($message) use($user)

{$message->to($user->email, $user->username)->subject('your new pass');

});

return Redirect::route('home')->with('global', 'we have sent you an new password');


}
}

}

return Redirect::route('account-change-password')->with('global', 'could not reset password');
}


public function getRecover($code){

$user = User::where('code', '=', $code)->where('password_temp', '!=', '');

if($user->count()){

$user = $user->first();

$user->password = $user->password_temp;
$user->password_temp = '';
$user->code = '';

if($user->save()){

return Redirect::route('home')->with('global', 'your account has been recoverd');

}

}

return Redirect::route('home')->with('global','could not recover you password');
}

最佳答案

应该没有问题,因为密码重置代码无论如何都应该绑定(bind)到用户帐户,使其成为复合 key ,因此是唯一的。

它只需要是随机的,而不是唯一的字符串,因为用户必须输入他们的电子邮件地址以及密码重置代码才能使其正常工作。所以如果 Bob 和 James 都有重置字符串 12345,那么他们输入它就不会发生冲突,因为 bob 会输入 bob@example.com 12345 而 james 会输入 james@acme.com 12345;因此它们都是独一无二的。

这并不是说您不应该使用随机字符串,您当然应该使用。字符串永远不应该被猜到。但至于是否完全独一无二,则无所谓。

关于php - str_random 在 laravel 中是唯一的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27018695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com