這篇文章主要介紹了關於PHP登入session的擷取,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
登入時記錄session
/** *執行登入 */public function doLogin(){$condition['name'] = $username = trim($_POST['username']);$condition['password'] = $password = md5(trim($_POST['password']));//稍後在加驗證碼驗證邏輯//$imgCode = $_POST['imgCode'];if (empty($username) || empty($password)) {$this->ajaxReturn(null,C("ERR_MSG_70"),"success:false");}$user = D("User")->relation(true)->where($condition)->find();if(empty($user)){$this->ajaxReturn(null,"使用者名稱或者密碼錯誤","success:false");}if(empty($user['apps']) && $user['role'] != UserModel::ADMIN){$this->ajaxReturn(null,'該使用者不屬於任何一條產品線,不允許登入,請聯絡管理員!','success:false');}if (empty($user)) {$this->ajaxReturn(null,C("ERR_MSG_70"),"success:false");}$defaultAppId = $user['defaultApp'] >= 0 ? $user['defaultApp'] : $user['apps'][0]['id'];foreach ($user['apps'] as $app){if ($app['id'] == $defaultAppId){$appName = $app['appName'];break;}}$session = array('uid'=>$user['id'],'username'=>$username,'role'=> $user['role'],'appId' => $defaultAppId,'appName'=>$appName);setSession($session);$this->ajaxReturn($data,"恭喜,登入成功!","success:true");}
具體頁面使用時,action中session的擷取
public function deploy() { $username = session('username'); $conditions = explode(",", $_POST['environment']); $envarr = array(); foreach ($conditions as $condition) { $envIds = D('Env')->field('name,IP') ->where("name like'" . $condition . "%'") ->select(); foreach ($envIds as $key => $envId) { $envIds[$key]['mem'] = $this->getCPUMEM($envId['IP']); } $envIdsSort = $this->my_sort($envIds, 'mem', SORT_ASC, SORT_STRING); // $envIds=array_remove($envIds,'mem'); $envarray['env'] = $condition; $envarray['smallIP'] = $envIdsSort[0]['IP']; $envarr[] = $envarray; } $_POST['environment'] = json_encode($envarr); $_POST['username']=$username; $resultnew = $this->request("localhost:8080/execute/isbuildingNew.html", $_POST, "POST"); // var_dump($resulenew); if ($resultnew == "building:true") { $this->ajaxReturn(1, '前一次構建進行中中,請稍候重試!', 'success:false'); } else { $result = $this->request("localhost:8080/execute/onlineDeployNew.html", $_POST, "POST"); if ($result) { $this->ajaxReturn(1, $result, 'success:true'); } else { $this->ajaxReturn(0, "提交構建失敗", 'success:false'); } } }
然後後端就能擷取到這個username了,soeasy!