欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

phpcms微博登陆后,不要二次注册,出现会话过时的详解

程序员文章站 2022-04-29 15:48:00
...
phpcms微博登陆后,不要二次注册,出现会话过时,
思路是 微博登录后,直接把用户昵称等写入数据库,
然后再执行一次,判断是否注册过,但是提示登录成功之后,总提示一次会话过期.再次登录后正常.
代码有点长,麻烦看下.

phpcms微博登陆后,不要二次注册,出现会话过时的详解

public function public_sina_login() {
		define('WB_AKEY', pc_base::load_config('system', 'sina_akey'));
		define('WB_SKEY', pc_base::load_config('system', 'sina_skey'));
		define('WEB_CALLBACK', APP_PATH.'index.php?m=member&c=index&a=public_sina_login&callback=1');
		pc_base::load_app_class('saetv2.ex', '' ,0);
		$this->_session_start();

		if(isset($_GET['callback']) && trim($_GET['callback'])) {
			$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
			if (isset($_REQUEST['code'])) {
				$keys = array();
				$keys['code'] = $_REQUEST['code'];
				$keys['redirect_uri'] = WEB_CALLBACK;
				try {
					$token = $o->getAccessToken('code', $keys);
				} catch (OAuthException $e) {
				}
			}
			if ($token) {
				$_SESSION['token'] = $token;
			}
			$c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token'] );
			$ms  = $c->home_timeline(); // done
			$uid_get = $c->get_uid();
			$uid = $uid_get['uid'];
			$me = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
			if(CHARSET != 'utf-8') {
				$me['name'] = iconv('utf-8', CHARSET, $me['name']);
				$me['location'] = iconv('utf-8', CHARSET, $me['location']);
				$me['description'] = iconv('utf-8', CHARSET, $me['description']);
				$me['screen_name'] = iconv('utf-8', CHARSET, $me['screen_name']);
			}
			if(!empty($me['id'])) {
				//检查connect会员是否绑定,已绑定直接登录,未绑定提示注册/绑定页面
				$where = array('connectid'=>$me['id'], 'from'=>'sina');
				$r = $this->db->get_one($where);

				//connect用户已经绑定本站用户
				if(!empty($r)) {
					//读取本站用户信息,执行登录操作

					$password = $r['password'];
					$this->_init_phpsso();
					$synloginstr = $this->client->ps_member_synlogin($r['phpssouid']);
					$userid = $r['userid'];
					$groupid = $r['groupid'];
					$username = $r['username'];
					$nickname = empty($r['nickname']) ? $username : $r['nickname'];
					$this->db->update(array('lastip'=>ip(), 'lastdate'=>SYS_TIME, 'nickname'=>$me['name']), array('userid'=>$userid));

					if(!$cookietime) $get_cookietime = param::get_cookie('cookietime');
					$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
					$cookietime = $_cookietime ? TIME + $_cookietime : 0;

					$phpcms_auth = sys_auth($userid."\t".$password, 'ENCODE', get_auth_key('login'));

					param::set_cookie('auth', $phpcms_auth, $cookietime);
					param::set_cookie('_userid', $userid, $cookietime);
					param::set_cookie('_username', $username, $cookietime);
					param::set_cookie('_groupid', $groupid, $cookietime);
					param::set_cookie('cookietime', $_cookietime, $cookietime);
					param::set_cookie('_nickname', $nickname, $cookietime);
					$forward = isset($_GET['forward']) && !empty($_GET['forward']) ? $_GET['forward'] : 'index.php?m=member&c=index';
						showmessage(L('login_success').$synloginstr, $forward);

				} else {
					//弹出绑定注册页面
					$_SESSION = array();
					$_SESSION['connectid'] = $me['id'];
					$_SESSION['from'] = 'sina';
					$connect_username = $me['name'];
					//SELECT max(userid) maxid FROM user

					$where="SELECT MAX(phpssouid) maxid FROM `v9_member` WHERE 1";
					$info = $this->db->query($where);
					$row = mysql_fetch_object($info);
						//	$row =Myspl_fetch_array($info,MYSQL_NUM);
					$maxid = $row->maxid;
					$phpssouid = $maxid+1;

					//写入数据库
					$this->db->insert(array('nickname'=>$me['name'],'regip'=>ip(),'regdate'=>SYS_TIME,'img'=>$me['avatar_large'],'phpssouid'=>$phpssouid,'password'=>'','groupid' =>'2','lastdate'=>SYS_TIME, 'lastip'=>ip(),'connectid'=>$me['id'],'from'=>'sina'));

					//加载用户模块配置
					$member_setting = getcache('member_setting');
					if(!$member_setting['allowregister']) {
						showmessage(L('deny_register'), 'index.php?m=member&c=index&a=login');
					}


					//获取用户siteid
					$siteid = isset($_REQUEST['siteid']) && trim($_REQUEST['siteid']) ? intval($_REQUEST['siteid']) : 1;
					//过滤非当前站点会员模型
					$modellist = getcache('member_model', 'commons');
					foreach($modellist as $k=>$v) {
						if($v['siteid']!=$siteid || $v['disabled']) {
							unset($modellist[$k]);
						}
					}
					if(empty($modellist)) {
						showmessage(L('site_have_no_model').L('deny_register'), HTTP_REFERER);
					}

					$modelid = 10; //设定默认值
					if(array_key_exists($modelid, $modellist)) {
						//获取会员模型表单
						require CACHE_MODEL_PATH.'member_form.class.php';
						$member_form = new member_form($modelid);
						$this->db->set_model($modelid);
						$forminfos = $forminfos_arr = $member_form->get();

						//万能字段过滤
						foreach($forminfos as $field=>$info) {
							if($info['isomnipotent']) {
								unset($forminfos[$field]);
							} else {
								if($info['formtype']=='omnipotent') {
									foreach($forminfos_arr as $_fm=>$_fm_value) {
										if($_fm_value['isomnipotent']) {
											$info['form'] = str_replace('{'.$_fm.'}',$_fm_value['form'], $info['form']);
										}
									}
									$forminfos[$field]['form'] = $info['form'];
								}
							}
						}

						//$formValidator = $member_form->formValidator;



						//zaizai再次调用
						define('WB_AKEY', pc_base::load_config('system', 'sina_akey'));
						define('WB_SKEY', pc_base::load_config('system', 'sina_skey'));
						define('WEB_CALLBACK', APP_PATH.'index.php?m=member&c=index&a=public_sina_login&callback=1');
						pc_base::load_app_class('saetv2.ex', '' ,0);
						$this->_session_start();
							$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
							if (isset($_REQUEST['code'])) {
								$keys = array();
								$keys['code'] = $_REQUEST['code'];
								$keys['redirect_uri'] = WEB_CALLBACK;
								try {
									$token = $o->getAccessToken('code', $keys);
								} catch (OAuthException $e) {
								}
							}
							if ($token) {
								$_SESSION['token'] = $token;
							}
							$c = new SaeTClientV2(WB_AKEY, WB_SKEY, $_SESSION['token']['access_token'] );
							$ms  = $c->home_timeline(); // done
							$uid_get = $c->get_uid();
							$uid = $uid_get['uid'];
							$me = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
							if(CHARSET != 'utf-8') {
								$me['name'] = iconv('utf-8', CHARSET, $me['name']);
								$me['location'] = iconv('utf-8', CHARSET, $me['location']);
								$me['description'] = iconv('utf-8', CHARSET, $me['description']);
								$me['screen_name'] = iconv('utf-8', CHARSET, $me['screen_name']);
							}
							if(!empty($me['id'])) {
								//检查connect会员是否绑定,已绑定直接登录,未绑定提示注册/绑定页面
//								$where = array('connectid' => $me['id'], 'from' => 'sina');
//								$r = $this->db->get_one($where);

								$where = " SELECT * FROM `phpcms`.`v9_member` WHERE `connectid` = ".$me['id']." AND `from` = 'sina' LIMIT 1 ";
								$r = $this->db->query($where);



								//connect用户已经绑定本站用户
								if (!empty($r)) {
									//读取本站用户信息,执行登录操作

									$password = $r['password'];
									$this->_init_phpsso();
									$synloginstr = $this->client->ps_member_synlogin($r['phpssouid']);
									$userid = $r['userid'];
									$groupid = $r['groupid'];
									$username = $r['username'];
									$nickname = empty($r['nickname']) ? $username : $r['nickname'];
									//$this->db->update(array('lastip' => ip(), 'lastdate' => SYS_TIME, 'nickname' => $me['name']), array('userid' => $userid));

									if (!$cookietime) $get_cookietime = param::get_cookie('cookietime');
									$_cookietime = $cookietime ? intval($cookietime) : ($get_cookietime ? $get_cookietime : 0);
									$cookietime = $_cookietime ? TIME + $_cookietime : 0;

									$phpcms_auth = sys_auth($userid . "\t" . $password, 'ENCODE', get_auth_key('login'));

									param::set_cookie('auth', $phpcms_auth, $cookietime);
									param::set_cookie('_userid', $userid, $cookietime);
									param::set_cookie('_username', $username, $cookietime);
									param::set_cookie('_groupid', $groupid, $cookietime);
									param::set_cookie('cookietime', $_cookietime, $cookietime);
									param::set_cookie('_nickname', $nickname, $cookietime);
									$forward = isset($_GET['forward']) && !empty($_GET['forward']) ? $_GET['forward'] : 'index.php?m=member&c=index';
									showmessage(L('login_success') . $synloginstr, $forward);

								}

							}


					}
					include template('member', 'connect');
				}
			} else {
				showmessage(L('login_failure'), 'index.php?m=member&c=index&a=login');
			}
		} else {
			$o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
			$aurl = $o->getAuthorizeURL(WEB_CALLBACK);
			include template('member', 'connect_sina');
		}
	}

回复内容:

会话过期 你是在那一行代码判断的?

我没去判断,我用的他自带的代码
思路是:
默认是判断 一次 //检查connect会员是否绑定,已绑定直接登录,未绑定提示注册/绑定页面

如果已经绑定,则直接登录,
没绑定,我会在写入数据库后,在调用一次判断(就是把判断的代码在复制过来一次,),这时候应该已经绑定了,他还是会在提示一次回话过期呢...

以上就是phpcms微博登陆后,不要二次注册,出现会话过时的详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!