1. 程式人生 > >登錄判斷

登錄判斷

用戶 function urn lse ajax fin 賬戶 login session

public function login(){
if (Request::instance()->isAjax()){
$name= Request::instance()->post(‘name‘);
$password = Request::instance()->post(‘password‘);
$result = Db::name(‘admin‘)->where([‘name‘=>$name])->find();
$inPWD=md5(md5($password));
if(empty($result)){
return [‘code‘=>-1,‘msg‘=>‘用戶不存在‘];
}

//判斷賬戶狀態 0為鎖定
if($result[‘status‘] == 0){
if((time() - $result[‘last_time‘]) > 300){
//過了鎖定時間 恢復正常狀態
$count[‘status‘] = 1;
Db::name(‘admin‘)->where([‘name‘ => $name])->update($count);
}else{
return [‘code‘=>-1,‘msg‘=>‘賬號或密碼錯誤超過3次,請5分鐘之後登錄!‘];
}
}

if($result[‘password‘]==$inPWD) {
Db::name(‘admin‘)->where([‘name‘ => $name])->update([‘ip‘ => Request::instance()->ip(), ‘last_time‘ => time()]);
session(‘admin‘, $result);
\session(‘last_time‘, time() + 1800);
return [‘code‘ => 0, ‘msg‘ => ‘登錄成功!‘];
}else{
if($result[‘count‘] < 3){
// 錯誤小於三次 字段值增加
$count[‘count‘] = $result[‘count‘]+1;
Db::name(‘admin‘)->where([‘name‘ => $name])->update($count);
}else{
// 錯誤次數大於3時 屬性恢復 清空 鎖定
$count[‘count‘] = 0;
$count[‘last_time‘] = time();
$count[‘status‘] = 0;
Db::name(‘admin‘)->where([‘name‘ => $name])->update($count);
}
return [‘code‘=>-1,‘msg‘=>‘密碼錯誤‘];
}
}
return view();
}


︿( ̄︶ ̄)︿?? 反正我是看懂了……

登錄判斷