Home > Backend Development > PHP Tutorial > Obtaining PHP login session

Obtaining PHP login session

不言
Release: 2023-03-25 07:24:01
Original
4510 people have browsed it

This article mainly introduces the acquisition of PHP login session, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Obtaining PHP login session

Record session when logging in

	
	/**
	 *	执行登录
	 */
	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");

	}
Copy after login

When using a specific page, get the session in the action

 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');
            }
        }
    }
Copy after login

Then the backend can get the username, so easy!

Related recommendations: PHP login to implement the remember me function

Related topic recommendations:php session ( Including pictures, texts, videos, cases)

The above is the detailed content of Obtaining PHP login session. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template