php - is str_random unique in laravel? -
i use str_random(60)
function generate password reset code. question if thousand of people asked resetting password code unique or duplicated?
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 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 password'); }
there shouldn't issue, since password reset code should tied user account anyway, making composite key, , therefore unique.
all needs random, not unique string, since user should have enter email address password reset code in order work. if bob , james both has reset string of 12345, them entering not conflict, since bob enter bob@example.com 12345 , james enter james@acme.com 12345; therefore both unique.
that not shouldn't have random strings, should. string should never guessable. whether unique, not matter.
Comments
Post a Comment