QQ 登录成功后返回登录前的页面,这个功能实现起来没有我想象中复杂,大体思路就是点击 QQ 登录按钮时用 js 将当前页面地址写入到 Cookie 中
然后在 QQ 登录的返回页面,像我设置的返回页面是首页,就在主题的 index.php 文件加一个判断,如果 referer 是 QQ 授权页面,就跳转到 Cookie 存储的地址
代码很简单,这里简单记录一下
登录按钮相关代码:
//在登录框添加按钮 function login_button() { echo ' <script> function setCookie(name,value){ var Days = 1; var exp = new Date(); exp.setTime(exp.getTime() + Days*24*60*60*30); document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString(); } function qqlogin_click(){ var current_url = window.location.href; var url_head = "https://www.wmjrc.com/wp-login.php?redirect_to="; var qq_redirect_url = current_url.split(url_head)[1]; setCookie("qq_redirect_url",qq_redirect_url) window.location.href="https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&client_id=101571082&state=6be89e3cebe2a9fb0a0baf0ca9b68543&scope=get_user_info&redirect_uri=https%3A%2F%2Fwww.wmjrc.com%2Findex.php%3Fconnect%3Dqq%26action%3Dcallback"; } </script> <a title="QQ 登录" onclick = "qqlogin_click()"><img class="aligncenter wp-image-1885" style="cursor: pointer;" src="https://www.wmjrc.com/wp-content/uploads/2019/03/201903201917197.png" alt="" width="30" height="30" /></a> <a title="微信登录" href="https://www.wmjrc.com/wechat.html" target="_blank" rel="noopener noreferrer"><img class="aligncenter wp-image-1886" src="https://www.wmjrc.com/wp-content/uploads/2019/03/2019032019171958.png" alt="" width="30" height="30" /></a>'; } add_action('login_form', 'login_button');
index.php 添加代码:
$url_array = $_SERVER["HTTP_REFERER"]; echo "<script>console.log('$url_array')</script>"; if(strpos($url_array,'https://graph.qq.com/oauth2.0/show?which=Login') !== false || strpos($url_array,'https://xui.ptlogin2.qq.com/cgi-bin/xlogin?') !== false){ $qq_redirect_url = $_COOKIE["qq_redirect_url"]; echo "<script>window.location.href = decodeURIComponent('$qq_redirect_url')</script>"; }