首页  >  文章  >  后端开发  >  PHP登录session的获取

PHP登录session的获取

不言
不言原创
2018-04-28 15:45:454480浏览

这篇文章主要介绍了关于PHP登录session的获取,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下

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!

相关推荐:PHP登录实现记住我功能

相关专题推荐php session (包含图文、视频、案例)

以上是PHP登录session的获取的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn