微信授权登录

xiaoxiao2021-02-28  66

1. html页面,点击微信登录授权,弹出授权页面

<?php session_start(); ?>

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="./jquery-3.2.1.min.js"></script> <script>   $(document).ready(function()     {         $('#wechat').click(function(){             var url     = encodeURIComponent("http://weixin.xiaowei360.com/deal.php");             var appid   = '公众号appid';             var state   = 123;//自己设置             window.location.href="https://open.weixin.qq.com/connect/qrconnect?appid="+appid+"&redirect_uri="+url+"&response_type=code&scope=snsapi_login&state="+state+"#wechat_redirect";         });     }); </script> <title>weshare</title> </head> <body>     <div>         欢迎您 <?php if($_SESSION['wxname']) echo $_SESSION['wxname'];?>     </div>   <div id="wechat"><h2>微信登录授权</h2></div> </body>

</html>

2.php处理文件,获取code,通过code获得 access_token + openid,再通过access_token + openid 获得用户所有信息,结果全部存储在$infoArray里,信息与自己数据库做匹配,若已经授权则直接登录且对应会员信息;若未授权进入登录页面,将登录授权标识openid存储在数据库,下次授权后直接登录

<?php   session_start();         $code = $_GET["code"];        $state = $_GET["state"];         $appid = "公众号appid";         $secret = "秘钥";         //通过code获得 access_token + openid         $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $secret . "&code=" . $code . "&grant_type=authorization_code";         $jsonResult = file_get_contents($url);         $resultArray = json_decode($jsonResult, true);         $access_token = $resultArray["access_token"];         $openid = trim($resultArray["openid"]);         //通过access_token + openid 获得用户所有信息,结果全部存储在$infoArray里         $infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid;         $infoResult = file_get_contents($infoUrl);         $infoArray = json_decode($infoResult, true);         $oauth_id = trim($infoArray["unionid"]);                  //设置数据库变量         if($oauth_id){             $db_host   = '数据库对应服务器IP+端口';               $db_user   = '用户名';                    $db_passw = '密码';               $db_name  = '数据库名';                      $conn = mysqli_connect($db_host,$db_user,$db_passw,$db_name);                if(!$conn){                 die("错误:".mysqli_connect_error());             }             mysqli_set_charset("utf8");                $sql = "select * from 表名 where 微信用户唯一标识字段 = '".$oauth_id."'";             $result = mysqli_query($conn,$sql);             if($row = mysqli_fetch_assoc($result)){                 $_SESSION['wxname'] = $row['username'];                 mysqli_close();                //已经授权直接登录状态                 header("Location: http://www.baidu.com");             }else{

//未授权进入登录界面,带上标识信息,用户登录同时提交标识信息至数据库,下次授权匹配直接登录                 mysqli_close();                 header("Location: http://www.baidu.com/login.php?微信用户唯一标识字段=".$oauth_id);             }                      }else{             echo "<h1>看看授权是不是有问题了</h1>";         }          ?>                

转载请注明原文地址: https://www.6miu.com/read-37085.html

最新回复(0)