°

php怎么获取当前登录用户信息

2019年课程规划及价格说明


PHP实战视频教程大全

php获取当前登录用户信息在php开发的系统中可以说是随处可见

下面我们就把自己在开发项目时候的核心代码那处理给大家参考

/**
	 * 获取当前登录用户
	 */
    public static function getLoginUser() {
		global $_config;
		$c_auth = false;
		$c_saltkey = false;
		$c_userinfo = false;
		foreach ($_COOKIE as $k => $v) {
			if (strpos($k, CoreFunction::$cookie_pre) === 0) {
				if (strpos($k, '_auth')) {
					$c_auth = $v;
				} elseif (strpos($k, '_saltkey')) {
					$c_saltkey = $v;
				} elseif (strpos($k, '_userinfo')) {
					$c_userinfo = $v;
				}
			}
		}
		if ($c_auth && $c_saltkey) {
			$c_auth = CoreFunction::daddslashes(explode("\t", CoreFunction::authcode($c_auth, 'DECODE', $c_saltkey)));
			list($discuz_pw, $discuz_uid) = empty($c_auth) || count($c_auth) < 2 ? array('', '') : $c_auth;
		}
		if ($discuz_pw && $discuz_uid) {
			// cookie 能解开,就认为是成功的登录状态
			$uid = $discuz_uid;
			$try_member = new Data_Try_Member();
			$user = $try_member->getInfo(array("uid"=>$uid), "");
			if ($user) {
				return $user[0];
			}
		}
		if ($c_userinfo) {
			$userinfo = json_decode($c_userinfo, true);
			if ($userinfo && $discuz_uid && $discuz_uid == $userinfo['uid']) {
				$userinfo['avatar'] = $_config['sso']['sych_user_info_avatar'] . "?uid={$discuz_uid}&size=middle";
				$info = array(
					'uid' => $userinfo['uid'],
                    "cyid" => '',
                    "username" => $userinfo['username'],
                    "avatar" => $userinfo['avatar'],
                    "sys_dateline" => time(),
                    "sys_status" => 0,
                    "sys_ip" => CoreFunction::getClientIp(),
                );
                $new_member = new Data_Try_Member($info);
                $uid = $new_member->create();
				return $info;
			}
		}
打赏
  喜欢