下面這段PHP後台登陸代碼實現免登陸
如果後台管理員帳號為admin,密碼為123456請問下面這段代碼如何?免登陸,麻煩知道的大哥直接給代碼謝謝了
PHP code
display(APP_PATH.'/Public/admin/login.html'); } //產生驗證碼 public function vcode(){ import("ORG.Util.Image"); Image::buildImageVerify();//6,0,'png',1,20,'verify' } //登入檢測 public function check(){ if(empty($_POST['user_name'])){$this->error('帳號必須!');} if(empty($_POST['user_pwd'])){$this->error('密碼必須!');} if(function_exists('gd_info')){ if(empty($_POST['verify'])){$this->error('驗證碼必須!');} if($_SESSION['verify']!=md5($_POST['verify'])){$this->error('驗證碼錯誤!');} } //產生認證條件 $map=array(); //支援使用綁定帳號登入 $map['admin_name']=$_POST['user_name']; //$map["user_status"]=array('gt',0);//狀態 $rs=D("Admin.Admin"); $authInfo=$rs->where($map)->find(); //使用使用者名稱、密碼和狀態的方式進行認證 if(false === $authInfo) { $this->error('帳號不存在或已禁用!'); }else { if($authInfo['admin_pwd']!=md5($_POST['user_pwd'])){ $this->error('密碼錯誤!'); } // 緩衝存取權限 $_SESSION[C('USER_AUTH_KEY')]=$authInfo['admin_id']; $_SESSION['admin_ok']=$authInfo['admin_ok']; $_SESSION['admin_name']=$authInfo['admin_name']; //$_SESSION['email']=$authInfo['admin_email']; //$_SESSION['lastLoginTime']=$authInfo['admin_logintime']; //$_SESSION['login_count']=$authInfo['admin_count']; //if($authInfo['user_name']=='admin') { //$_SESSION['administrator'] = true; //} //儲存登入資訊 $ip=get_client_ip(); $data=array(); $data['admin_id']=$authInfo['admin_id']; $data['admin_logintime']=time(); $data['admin_count']=array('exp','admin_count+1'); $data['admin_ip']=get_client_ip(); $rs->save($data); redirect('index.php?s=Admin-Index'); } } // 使用者登出 public function logout(){ if(isset($_SESSION[C('USER_AUTH_KEY')])) { unset($_SESSION[C('USER_AUTH_KEY')]); unset($_SESSION); session_destroy(); $this->assign('jumpUrl','index.php?s=Admin-Login'); $this->success('登出成功!'); }else { $this->error('已經登出!'); } } }?>
------解決方案--------------------
改寫 index 方法為
PHP code
public function index(){ if ($_SESSION[C('USER_AUTH_KEY')]){ redirect("index.php?s=Admin-Index"); } //$this->display(APP_PATH.'/Public/admin/login.html'); $_POST['user_name'] = 'admin'; $_POST['user_pwd'] = '123456'; $_POST['verify'] = 1; $_SESSION['verify'] = md5($_POST['verify']); $this->check(); }