CREATE TABLE `oauth_users` ( `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `oauth_provider` VARCHAR(10), `oauth_uid` text, `oauth_token` text, `oauth_secret` text, `username` text, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
<?php
session_start();
//if( isset($_SESSION['last_key']) )
header("Location: weibolist.php");
include_once( 'config.php' );
include_once( 'weibooauth.php' );
// 创建 sinaOAuth 对象实例
$sinaOAuth = new WeiboOAuth( WB_AKEY , WB_SKEY );
$keys = $sinaOAuth->getRequestToken();
// Requesting authentication tokens, the parameter is the URL we will be redirected to
$aurl = $sinaOAuth->getAuthorizeURL( $keys['oauth_token'] ,false , 'http://t.yourtion.com/sina/callback.php');
// 保存到 session 中
$_SESSION['keys'] = $keys;
?>
<a href="<?=$aurl?>">Use Oauth to login</a>
<?php
session_start();
include_once ('config.php');
include_once ('weibooauth.php');
if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['keys']['oauth_token']) &&
!empty($_SESSION['keys']['oauth_token']))
{
// SinaOAuth 对象实例,注意新加入的两个参数
$sinaOAuth = new WeiboOAuth(WB_AKEY, WB_SKEY, $_SESSION['keys']['oauth_token'],
$_SESSION['keys']['oauth_token_secret']);
// 获取 access token
$access_token = $sinaOAuth->getAccessToken($_REQUEST['oauth_verifier']);
// 将获取到的 access token 保存到 Session 中
$_SESSION['access_token'] = $access_token;
// 获取用户信息
$user_info = $sinaOAuth->get('account/verify_credentials');
// 打印用户信息
mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PSSWORD);
mysql_select_db(DATABASE_DB_NAME);
//更换成你的数据库连接,在config.php中
if (isset($user_info->error) or empty($user_info['id']))
{
// Something's wrong, go back to square 1
header('Location: index.php');
} else
{
// Let's find the user by its ID
$sql = "SELECT * FROM oauth_users WHERE oauth_provider='sina' AND oauth_uid=" .$user_info['id'];
$query = mysql_query($sql);
$result = mysql_fetch_array($query);
// If not, let's add it to the database
if (empty($result))
{
$sql = "INSERT INTO oauth_users (oauth_provider, oauth_uid, username, oauth_token, oauth_secret) VALUES ('sina', '" .
$user_info['id'] . "', '" . $user_info['screen_name'] . "', '" . $access_token['oauth_token'] .
"', '" . $access_token['oauth_token_secret'] . "')";
$query = mysql_query($sql);
$query = mysql_query("SELECT * FROM oauth_users WHERE id = ".mysql_insert_id());
$result = mysql_fetch_array($query);
} else
{
// Update the tokens
$query = mysql_query("UPDATE oauth_users SET oauth_token = '" . $access_token['oauth_token'] .
"', oauth_secret = '" . $access_token['oauth_token_secret'] .
"' WHERE oauth_provider = 'sina' AND oauth_uid = " . $user_info['id']);
}
$_SESSION['id']=$result['id'];
$_SESSION['username']=$result['username'];
$_SESSION['oauth_uid']=$result['oauth_uid'];
$_SESSION['oauth_provider']=$result['oauth_provider'];
$_SESSION['oauth_token']=$result['oauth_token'];
$_SESSION['oauth_secret']=$result['oauth_secret'];
header('Location: update.php');
}
} else
{
// 数据不完整,转到上一步
header('Location: index.php');
}
?>
<?php
include_once ('config.php');
include_once ('weibooauth.php');
session_start();
if(!empty($_SESSION['username'])){
// User is logged in, redirect
header('index.php');
}
?>
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="zh-CN">
<head profile="http://gmpg.org/xfn/11">
<title>通过 OAuth 进行身份验证--Yourtion</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<h2>Hello <?=$_SESSION['username'] ?></h2>
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有