mrbs增加Jaccount登录
浏览量:2392
首先,先搞个jaccount.php把用户信息给调出来
<?php header("Content-type:text/html;charset=utf-8"); function request_post($url = '', $post_data = array()) { if (empty($url) || empty($post_data)){return false;} $o = ""; foreach ($post_data as $k => $v){$o .= $k . "=" . $v . "&";} $post_data = substr($o, 0, -1); $postUrl = $url; $curlPost = $post_data; $ch = curl_init();//初始化curl curl_setopt($ch, CURLOPT_URL, $postUrl); // 要访问的地址 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($ch, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); // Post提交的数据包 curl_setopt($ch, CURLOPT_TIMEOUT, 5); // 设置超时限制防止死循环 curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec($ch);//运行curl if (curl_errno($ch)){echo 'Errno' . curl_error($ch);}//捕抓异常 curl_close($ch); return $data; } function request_get($url = '') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer curl_setopt($ch, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 $data = curl_exec($ch);//运行curl if (curl_errno($ch)){echo 'Errno' . curl_error($ch);}//捕抓异常 curl_close($ch); return $data; } function logout(){ setcookie("UserName", NULL,time()-3600 ,'/'); setcookie("user", NULL,time()-3600,'/'); setcookie("jaccount", NULL, time()-3600,'/'); session_destroy(); if(!$_SERVER["HTTP_REFERER"]){ $HTTP_REFERER = '/'; }else{ $HTTP_REFERER = $_SERVER["HTTP_REFERER"]; } header("Location:".$HTTP_REFERER);die; } // if(!$_COOKIE['HTTP_REFERER']){ // $_COOKIE['HTTP_REFERER'] = $_SERVER["HTTP_REFERER"]; // } if(isset($_COOKIE["UserName"])){//登录成功后 header("Location:/");die; } if(isset($_GET['code'])) { $auth_code = $_GET['code']; $url = 'https://jaccount.sjtu.edu.cn/oauth2/token'; $post_data = array( 'grant_type' => 'authorization_code', 'code' => $auth_code, 'redirect_uri' => 'http://你的BS地址/jaccount.php', 'client_id' => '你的ID', 'client_secret' => '你的secret' ); $token_json = request_post($url, $post_data); $token_info = json_decode($token_json); $url = "https://api.sjtu.edu.cn/v1/me/profile?access_token=".$token_info->access_token; $usr_json = request_get($url); $usr_info = json_decode($usr_json); setcookie('jaccountID',$usr_info->entities[0]->code); setcookie('jaccountUid',$usr_info->entities[0]->account); setcookie('jaccountName',$usr_info->entities[0]->name); setcookie('jaccountDepartment',$usr_info->entities[0]->organize->name); setcookie('jaccountType',$usr_info->entities[0]->userType); setcookie("UserName", strtolower($usr_info->entities[0]->account), time()+42000); // echo $_COOKIE["UserName"]; header("Location:/"); }else{ header("Location: 你的ID&redirect_uri=http://你的BS地址/jaccount.php"); } ?>
找到session_cookie.inc,找到下面这行,把它的setcookie给注释掉,这个一上来就给删除cookie导致jaccount的cookie不能用:
/* Delete old-style cookies */
找到session_cookie.inc
if (isset($Action) && ($Action == "SetName")) 这个是注销使用的,下面增加: session_destroy(); //清除登录 if (!empty($_COOKIE) && isset($_COOKIE["UserName"])){ setcookie("UserName", '', time()-42000, $cookie_path); }
找到session_cookie.inc
在登录的地方调整下面的代码:
<form method="post" action="admin.php?sss=未登录"> <div> <input type="hidden" name="TargetURL" value="<?php echo htmlspecialchars($TargetURL) ?>"> <input type="hidden" name="Action" value="QueryName"> <input type="submit" value=" <?php echo get_vocab('login') ?> "> <?php if(!isset($_COOKIE['UserName'])){?> <a href='/jaccount.php'>JAccount Login</a> <?php }elseif(isset($_COOKIE['UserName'])){echo $_COOKIE['UserName'];}?> </div> </form>
神回复
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。