【菜鸡求助】公众号授权登录获取openid后,如何带参数跳转?

hgwyl 2018-02-28 12:24:52

公众号已设置好,可以正常获取和打印openid,两段代码如下

default.php

<?php
//scope=snsapi_base 实例
$appid='公众号id';
$redirect_uri = urlencode ( '获取openid页面链接地址' );
$url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=001#wechat_redirect";
header("Location:".$url);
exit;
?>


getuseropenid.php

<?php

$appid = "公众号id";
$secret = "公众号密匙";
$code = $_GET["code"];

//第一步:取全局access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret";
$token = getJson($url);

//第二步:取得openid
$oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
$oauth2 = getJson($oauth2Url);

//第三步:根据全局access_token和openid查询用户信息
$access_token = $token["access_token"];
$openid = $oauth2['openid'];
$get_user_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";
$userinfo = getJson($get_user_info_url);

function getJson($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
return json_decode($output, true);
}

?>

该页面中 echo("openid:$openid"); 可以正常打印出来



现在需要在getuseropenid.php中
1、判断是否成功获取openid
2、如获取成功,则跳转到指定页面aaa,asp?openid=xxoo,带参数

试过百度很多代码,基本大同小异,但是都没有成功。

麻烦哪位大神,给段代码,我再慢慢研究修改。
额外想请教下,这种带参数明文跳转,是否存在安全问题?

一点分,不成敬意,诚心求帮忙。
...全文
869 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
木秀猿林 2018-02-28
  • 打赏
  • 举报
回复
引用 8 楼 hgwyl 的回复:
[quote=引用 7 楼 w6248117 的回复:] [quote=引用 3 楼 hgwyl 的回复:] [quote=引用 1 楼 w6248117 的回复:] 你openid已经而已打印了也就是你获取成功了,你直接在将echo改成$url=你需要跳转的地址;header('location:'.$url);就可以了
用heaher的是直接跳转了吧? 这样getuseropenid.php中就不再执行获取openid了?[/quote] 这是我写的getuserinfo.php的代码你看看对你有没有帮助吧!xxx改成你自己的域名就可以 $code = $_GET['code']; //如果code存在则进入获取openid逻辑 if(!empty($code)){ $appid = "wxbab7ff902516fb70"; $secret = "1f8f7a49943901075ed6edd4e87a84aa"; //获取全局access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret"; $accesstokenres = getJson($url); //获取openid $oauthurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code"; $oauthres = getJson($oauthurl); if($oauthres['openid']){ //var_dump($oauthres); $url = "https://xxx.com/index.php?openid=".$oauthres['openid']; header('location:'.$url); }else{ echo "获取失败!!!"; } }else{ echo "无code参数,无法获取openid"; } function getJson($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } index.php页面代码 echo $_GET['openid']; exit();[/quote] 我在自己代码的function getJson前面加了这2句 $url = "https://xxx.com/index.php?openid=".$oauthres['openid']; header('location:'.$url); 能正常跳转和带参数了,谢谢 回头再试试用session[/quote] 没事,你可以用session保存也可以保存在文件中。然后在去文件中读取openid,我测试了因为php不是常驻内存的语言所以用全局变量无法保存。 只是保存文件比保存session会麻烦一些(可能需要考虑多用户多文件的问题等等,所以我个人建议还是保存session)。
hgwyl 2018-02-28
  • 打赏
  • 举报
回复
引用 7 楼 w6248117 的回复:
[quote=引用 3 楼 hgwyl 的回复:] [quote=引用 1 楼 w6248117 的回复:] 你openid已经而已打印了也就是你获取成功了,你直接在将echo改成$url=你需要跳转的地址;header('location:'.$url);就可以了
用heaher的是直接跳转了吧? 这样getuseropenid.php中就不再执行获取openid了?[/quote] 这是我写的getuserinfo.php的代码你看看对你有没有帮助吧!xxx改成你自己的域名就可以 $code = $_GET['code']; //如果code存在则进入获取openid逻辑 if(!empty($code)){ $appid = "wxbab7ff902516fb70"; $secret = "1f8f7a49943901075ed6edd4e87a84aa"; //获取全局access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret"; $accesstokenres = getJson($url); //获取openid $oauthurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code"; $oauthres = getJson($oauthurl); if($oauthres['openid']){ //var_dump($oauthres); $url = "https://xxx.com/index.php?openid=".$oauthres['openid']; header('location:'.$url); }else{ echo "获取失败!!!"; } }else{ echo "无code参数,无法获取openid"; } function getJson($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } index.php页面代码 echo $_GET['openid']; exit();[/quote] 我在自己代码的function getJson前面加了这2句 $url = "https://xxx.com/index.php?openid=".$oauthres['openid']; header('location:'.$url); 能正常跳转和带参数了,谢谢 回头再试试用session
木秀猿林 2018-02-28
  • 打赏
  • 举报
回复
引用 3 楼 hgwyl 的回复:
[quote=引用 1 楼 w6248117 的回复:] 你openid已经而已打印了也就是你获取成功了,你直接在将echo改成$url=你需要跳转的地址;header('location:'.$url);就可以了
用heaher的是直接跳转了吧? 这样getuseropenid.php中就不再执行获取openid了?[/quote] 这是我写的getuserinfo.php的代码你看看对你有没有帮助吧!xxx改成你自己的域名就可以 $code = $_GET['code']; //如果code存在则进入获取openid逻辑 if(!empty($code)){ $appid = "wxbab7ff902516fb70"; $secret = "1f8f7a49943901075ed6edd4e87a84aa"; //获取全局access_token $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret"; $accesstokenres = getJson($url); //获取openid $oauthurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code"; $oauthres = getJson($oauthurl); if($oauthres['openid']){ //var_dump($oauthres); $url = "https://xxx.com/index.php?openid=".$oauthres['openid']; header('location:'.$url); }else{ echo "获取失败!!!"; } }else{ echo "无code参数,无法获取openid"; } function getJson($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); return json_decode($output, true); } index.php页面代码 echo $_GET['openid']; exit();
hgwyl 2018-02-28
  • 打赏
  • 举报
回复
引用 4 楼 w6248117 的回复:
[quote=引用 2 楼 oHuaMeiWeiDeTang 的回复:] openid放到get参数太不安全了吧
你可以使用全部变量或者存储到session中[/quote] 啊……把openid放到session里然后跳转? 好像是个解决办法哦?
hgwyl 2018-02-28
  • 打赏
  • 举报
回复
引用 2 楼 oHuaMeiWeiDeTang 的回复:
openid放到get参数太不安全了吧
由于是菜鸡,似乎看见过说把这个放到链接上明文跳转不安全? 我是需要获取openid然后作为判定方式,绑定表中的user,如果没有openid的话实现不了。 一般用什么办法解决呢?
木秀猿林 2018-02-28
  • 打赏
  • 举报
回复
引用 2 楼 oHuaMeiWeiDeTang 的回复:
openid放到get参数太不安全了吧
你可以使用全部变量或者存储到session中
hgwyl 2018-02-28
  • 打赏
  • 举报
回复
引用 1 楼 w6248117 的回复:
你openid已经而已打印了也就是你获取成功了,你直接在将echo改成$url=你需要跳转的地址;header('location:'.$url);就可以了
用heaher的是直接跳转了吧? 这样getuseropenid.php中就不再执行获取openid了?
话梅味的糖 2018-02-28
  • 打赏
  • 举报
回复
openid放到get参数太不安全了吧
木秀猿林 2018-02-28
  • 打赏
  • 举报
回复
你openid已经而已打印了也就是你获取成功了,你直接在将echo改成$url=你需要跳转的地址;header('location:'.$url);就可以了

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧