【发布时间】:2014-03-06 10:59:57
【问题描述】:
我在网上学习这个登录教程,用户登录后可以选择更改密码,并且给出的注释是
if ($validator->fails())
{
return Redirect::route('account-change-password')
->withErrors($validator);
}
else
{
$user = User::find(Auth::user()->id);
$old_password = Input::get('old_password');
$password = Input::get('password');
if(Hash::check($old_password, $user->getAuthPassword()))
{
$user->password = Hash::make($password);
if($user->save())
{
return Redirect::route('home')
->with('global', 'Your Password has been changed');
}
}
else
{
return Redirect::route('account-change-password')
->with('global', 'Your old password is incorrect');
}
}
在教程里面的else $user = User::find(Auth::user()->id);被立即使用,然后检查密码的if语句使用$user = User::find(Auth::user()->id);
当我在观看教程后尝试在我的 on 上执行此操作时,我的做法有所不同。
if ($v->fails())
{
return Redirect::route('account-change-password')
->withErrors($v)
->with('global', 'Please check the errors in red');
}
else
{
if (Hash::check(Input::get('old_password'),Auth::user()->password))
{
$user = User::find(Auth::user()->id);
$user->password = Hash::make(Input::get('new_password'));
if ($user->save())
{
return Redirect::route('home')
->with('global', 'Password Changed.');
}
}
return Redirect::route('account-change-password')
->with('global', 'Old password is incorrect');
}
我使用 if 语句立即使用 Auth::user()->password) 检查了密码和数据库中的密码,并将 $user = User::find(Auth::user()->id); 放入 if 语句中,以便我可以访问 $user->password 并更改密码。
我知道做事总是有很多不同的方法,但我只是想知道我这样做的方式将来是否会出现某种我现在没有意识到的问题,最好记住并坚持教程教的方式。
【问题讨论】:
标签: php authentication if-statement laravel passwords