您的位置:首页 > 移动开发 > 微信开发

微信扫码登录验证PHP代码(不用开放平台)

2017-05-04 22:28 471 查看
public function wxqrcheckAction(){
if(g('ajaxchk')!=''){ //前端页面显示二维码并AJAX定时检查已否已扫码验证

if(isset($_SESSION['wxqrchk'])){
$msg='';
if(isset($_SESSION['user'])){
$msg=$_SESSION['user']['username'];
}
$this->ajaxReturn(array('code'=>1,'username'=>$msg));
}else{
$this->ajaxReturn(array('code'=>0));
}

die();

}else if(g('wxauth')!=''){ //扫码后访问
$_SESSION['chksid']=g('wxauth');
WXAuthController::Auth('/Public/wxqrcheck/redirect/'.g('wxauth'),1);
die("wxauth end...");
}else if(g('redirect')!=''){
$openid=WXAuthController::GetOpenid(); //获取微信OPENID
$user=ORM::for_table('sys_user')->where('openid',$openid)->find_one();
if($user){
$chksid=$_SESSION['chksid'];
session_destroy(); //thinkphp等开始已经session_start了
session_id($chksid);
session_start();//这个函数必须在session_id()之后

$_SESSION['user']=$user->as_array();
if($_SESSION['user']['username']=='admin' || $_SESSION['user']['username']=='adminisrator'){
$_SESSION['isadmin']=true;
}else{
unset( $_SESSION['isadmin']);
}
$_SESSION['islogin']=true;
$_SESSION['wxqrchk']=true;
echo "扫码登录成功<br/>";

}else{
echo "没有这个用户 openid:".$openid;
}

die("手机端验证完成");
}else{
$sessionid=session_id();
$qrurl="http://XXXXX.COM/Public/wxqrcheck/wxauth/".$sessionid;
$this->assign('qrurl',$qrurl);
$this->display();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息