ӯѹʱ

ӯѹʱ

̣ԭ 1.ͨcodeaccess_tokenͨȨȡûϢûu_id(u_idںĵ¼sina_idǸҪԼ) 2.ѯ¼ûsina_id,2һûƽ̨ѾʺţʱҪƽ̨磺ƽ̨ûǣuser_regûid󶨵¼ǣthird_loginȻÿͻ¼; ûƽ̨ûʺţתעҳעᣬעͬʱϢдuer_regͬʱҲûsina_idд¼а; 3.ѯ¼(third_login)ûsina_id,ٲѯû(user_reg)Ѿֱӵ¼ûмʾûȥ伤ʺš

濪ʼ꽲裺һApp keyApp secretַhttp://open.weibo.com/ ҳվWEBȥͺˣͨõApp Key App Secret£App Key1428003339App Sercetf1c6177a38b39f764c76a1690720a6dcصַhttp://test.com/callback.php

˵ʺžDzʺţڿʱʺԣʺ޷¼޷ϢġǰϹ¿̣ҪġֻҪ˼·ˣʣ¾ôʵ˼롣

ڶSDKphpģصַhttp://code.google.com/p/libweibo/downloads/list5ļһsaetv2.ex.class.phpֻҪļ

1.һ¼Ա洢¼Ϣu_id,QQopenidǶΨһģʶûǸ洢

ƴ :CREATE TABLE IF NOT EXISTS `third_login` ( `user_id` INT(6) NOT NULL, `sina_id` BIGINT(16) NULL, `qq_id` varchar(64) NULL, PRIMARY KEY (`user_id`), UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC), INDEX `sina_id` (`sina_id` ASC), INDEX `index4` (`qq_id` ASC))ENGINE = MyISAMDEFAULT CHARACTER SET = utf8COLLATE = utf8_binCOMMENT = '¼'

˵ƽ̨صu_id,ûΨһʶҰΪsina_id,user_idǹƽ̨ûuser_regidģuser_regﲻг԰ʵĿƼIJphpmyadmin,MySQL Workbench,㡣ֻҪ˵¼ӿڣǿ԰qq_idֶȥ

2.дļapplication½һļsina_conf.php,Ѹ뵽App Key App Secretдȥ£

ƴ :<?php$config["sina_conf"] = array( "App_Key" => '1428003339', "App_Secret" =>'f1c6177a38b39f764c76a1690720a6dc', "WB_CALLBACK_URL" => 'http://test.com/callback.php');

3.oauth֤࣬Ѹsaetv2.ex.class.phpļƵapplication/libraries¡˵ǷdzҪ࣬¼ȨȡûϢҪõеķûûȥˣԭⲻճapplication/libraries¡

4.д΢¼ࣨQQ¼ҲãQQ¼Ҳװһˣֻ˵¼ӿڣҲӰ죩application/models½һļthird_login_model.php룺

ƴ :<?php/*** Description of third_login_model*ӿȨ¼model* @author*/class third_login_model extends CI_Model{ //put your code here private $sina=array(); private $qq =array(); private $users =''; private $third=''; public function __construct() { parent::__construct();// $this->l = DIRECTORY_SEPARATOR; $this->load->database(); $this->load->library('session'); include_once APPPATH."/libraries"."/saetv2.ex.class.php"; $this->third = $this->db->'third_login';//¼ $this->users = $this->db->'user_reg';//Ŀû $this->config->load("sina_conf"); $this->sina= $this->config->item("sina_conf"); } /** * @uses : ΢¼ * @param : * @return : $sina_url----¼ַ */ public function sina_login(){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); $sina_url = $obj->getAuthorizeURL( $this->sina['WB_CALLBACK_URL'] ); return $sina_url; } /** * @uses : ¼ͨصcodeֵȡtokenʵȨɣȻȡûϢ * @param : $code * @return : $user_message--ûϢ */ public function sina_callback($code){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); if (isset($code)) { $keys = array(); $keys['code'] = $code; $keys['redirect_uri'] = $this->sina['WB_CALLBACK_URL']; try { $token = $obj->getAccessToken( 'code', $keys ) ;//Ȩ } catch (OAuthException $e) { } } $c = new SaeTClientV2($this->sina['App_Key'], $this->sina['App_Secret'], $token['access_token']); $ms =$c->home_timeline(); $uid_get = $c->get_uid();//ȡu_id $uid = $uid_get['uid']; $user_message = $c->show_user_by_id($uid);//ȡûϢ return $user_message; } /** * @uses : ѯ¼ * @param : $where * @return : ¼û¼ */ public function select_third($where) { $result = false; $this->db->select(); $this->db->from($this->third); $this->db->where($where); $query = $this->db->get(); if($query){ $result = $query->row_array(); } return $result; } /*- * @uses : sina---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_name($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.sina_id={$where}"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; } /** * @uses : qq---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_qqname($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.qq_id='{$where}'"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; }

/** * @uses : û͵¼Ϣ * @param : $datas * @return : */ public function binding_third($datas) { if (!is_array($datas)) show_error ('wrong param'); if($datas['sina_id']==0 && $datas['qq_id']==0) return; $resa =''; $resb =''; $resa = $this->select_third(array("user_id"=>$datas['user_id'])); $temp =array( "user_id"=>$datas['user_id'], "sina_id"=>$resa['sina_id']!=0 ? $resa['sina_id'] : $datas['sina_id'], "qq_id" => $resa['qq_id']!=0 ? $resa['qq_id'] : $datas['qq_id'], ); if($resa){ $resb = $this->db->update($this->third, $temp,array("user_id"=>$datas['user_id'])); }else{ $resb = $this->db->insert($this->third,$temp); } if($resb) { $this->session->unset_userdata('sina_id');//ע $this->session->unset_userdata('qq_id');//ע } return $resb; }}

˵:codeļcallback.phpģ7ϸ롣ļmodelݱˣǿͼļˡ

5.д¼ application/controllers£login.phpļ(Լȡ)룺

ƴ :<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*** Description of index* @author victory*/class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('login_model','login');//DZĿû¼࣬ṩԭ룬ΪͬĿͬɸĿԼװ $this->load->model("third_login_model","third"); $this->load->library('session'); }

public function index() { header("content-type: text/html; charset=utf-8"); $this->load->model("third_login_model","third");//˵¼ӿ $datas['sina_url'] = $this->third->sina_login();//еsina_login $this->load->view("index.php",$datas);//ȡͼļ

}

public function callback(){ header("content-type: text/html; charset=utf-8"); $this->load->model("user_reg_model","user_reg"); $code = $_REQUEST['code'];//codeֵļcallback.php $arr =array(); $arr = $this->third->sina_callback($code);//ͨȨȡûϢu_id $res = $this->third->select_third(array("sina_id"=>$arr['id'])); if(!empty($res)){//ûʺż¼жʺǷ񼤻 $user_info = $this->user_reg->user_detect(array("id"=>$res['user_id']));//ѯû״̬user_detectDzѯûϢķҲ˵ˣlogin_model.php౾ṩҪԼȥװ if($user_info['status']){//status״̬жûʺǷ񼤻,user_regеֶstatus1Ϊδ0ΪѼ echo "<script>alert('˺δȥ伤');location='/login/index';</script>";die(); } $datas = $this->third->select_user_name($arr['id']);//󣬰Ϣдû͵¼ $uname = $datas['username'];//username,passworduser_regֶΣuser_regݱĹҲṩΪÿĿһҪʵĿ $password = $datas['password']; $this->load->model("login_model","login"); $this->login->validation($uname,$password);//validationǵ¼ҪҪڵ¼ʱ򣬽ûϢд¼ṩд¼Ĵ echo "<script>alert('¼ɹ!');location='/user_center'</script>";die(); }else{//ûûм¼ѯûǷƽ̨йʺţûתעᣬת¼ $this->session->set_userdata('sina_id',$arr['id']); echo "<script>if(!confirm('Ƿƽ̨עû')){location='/register/index'}else{location='/login'};</script>"; } }

public function login_validation(){ //¼ûid ,sina_id,qq_idļ¼ $third_info =array( "user_id" => $user_ser['id'], "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($third_info['sina_id']||$third_info['qq_id']) $this->third->binding_third($third_info); // }//

//עûϢдuser_regͬʱҲsina_idдthird_loginֻչʾ¼ӿûidݱĴclass Register extends CI_Controller {

public function __construct() { parent::__construct(); $this->load->library('session'); } public function reg() { $haha =array( "user_id" => $rs, "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($haha['sina_id']||$haha['qq_id']) $this->third->binding_third($haha); }}

6.ͼļ΢¼ťapplication/view½index.phpļ룺

ƴ :<html><head> <meta content="text/html; charset=utf-8"> <title>΢¼ӿ</title></head><body> <div><a href="<?=$sina_url?>"><img src="http://images.cnblogs.com/weibo_login.png" width="110" /></a></div></body></html>

˵ǸͼƬťͼƬڹأصַhttp://open.weibo.com/widget/loginbutton.php

7.صַǰڵ1ļļʱ˻صַhttp://test.com/callback.php callback.phpʲôطأҪںindex.phpͬλãapplicationҲͬġڿʼĿ¼½ļcallback.php룺

ƴ :<?php

/** To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates* and open the template in the editor.*///΢¼صļ·תƵlogin/callbackcodeֵȥ$code ='';$url = '';$str ='';$code = $_REQUEST['code'];$url = "/login/callback";

$str = "<!doctype html><html> <head> <meta charset=\"UTF-8\"> <title>Զת</title> </head><body>";$str .="<form action=\"{$url}\" method=\"post\" id=\"form\" autocomplete='off'>";$str .="<input type='hidden' name='code' value='{$code}'>";$str .="</form> </body> </html> <script type=\"text/javascript\"> document.getElementById('form').submit(); </script>";echo $str;

ʱindex.phpļʱ򣬻ῴһ΢ʺŵ¼ĵ¼ťťת΢¼ҳ棬Ҫ΢û,ͬIJҲ˵ˡ

̣ԭ 1.ͨcodeaccess_tokenͨȨȡûϢûu_id(u_idںĵ¼sina_idǸҪԼ) 2.ѯ¼ûsina_id,2һûƽ̨ѾʺţʱҪƽ̨磺ƽ̨ûǣuser_regûid󶨵¼ǣthird_loginȻÿͻ¼; ûƽ̨ûʺţתעҳעᣬעͬʱϢдuer_regͬʱҲûsina_idд¼а; 3.ѯ¼(third_login)ûsina_id,ٲѯû(user_reg)Ѿֱӵ¼ûмʾûȥ伤ʺš

濪ʼ꽲裺һApp keyApp secretַhttp://open.weibo.com/ ҳվWEBȥͺˣͨõApp Key App Secret£App Key1428003339App Sercetf1c6177a38b39f764c76a1690720a6dcصַhttp://test.com/callback.php

˵ʺžDzʺţڿʱʺԣʺ޷¼޷ϢġǰϹ¿̣ҪġֻҪ˼·ˣʣ¾ôʵ˼롣

ڶSDKphpģصַhttp://code.google.com/p/libweibo/downloads/list5ļһsaetv2.ex.class.phpֻҪļ

1.һ¼Ա洢¼Ϣu_id,QQopenidǶΨһģʶûǸ洢

ƴ :CREATE TABLE IF NOT EXISTS `third_login` ( `user_id` INT(6) NOT NULL, `sina_id` BIGINT(16) NULL, `qq_id` varchar(64) NULL, PRIMARY KEY (`user_id`), UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC), INDEX `sina_id` (`sina_id` ASC), INDEX `index4` (`qq_id` ASC))ENGINE = MyISAMDEFAULT CHARACTER SET = utf8COLLATE = utf8_binCOMMENT = '¼'

˵ƽ̨صu_id,ûΨһʶҰΪsina_id,user_idǹƽ̨ûuser_regidģuser_regﲻг԰ʵĿƼIJphpmyadmin,MySQL Workbench,㡣ֻҪ˵¼ӿڣǿ԰qq_idֶȥ

2.дļapplication½һļsina_conf.php,Ѹ뵽App Key App Secretдȥ£

ƴ :<?php$config["sina_conf"] = array( "App_Key" => '1428003339', "App_Secret" =>'f1c6177a38b39f764c76a1690720a6dc', "WB_CALLBACK_URL" => 'http://test.com/callback.php');

3.oauth֤࣬Ѹsaetv2.ex.class.phpļƵapplication/libraries¡˵ǷdzҪ࣬¼ȨȡûϢҪõеķûûȥˣԭⲻճapplication/libraries¡

4.д΢¼ࣨQQ¼ҲãQQ¼Ҳװһˣֻ˵¼ӿڣҲӰ죩application/models½һļthird_login_model.php룺

ƴ :<?php/*** Description of third_login_model*ӿȨ¼model* @author*/class third_login_model extends CI_Model{ //put your code here private $sina=array(); private $qq =array(); private $users =''; private $third=''; public function __construct() { parent::__construct();// $this->l = DIRECTORY_SEPARATOR; $this->load->database(); $this->load->library('session'); include_once APPPATH."/libraries"."/saetv2.ex.class.php"; $this->third = $this->db->'third_login';//¼ $this->users = $this->db->'user_reg';//Ŀû $this->config->load("sina_conf"); $this->sina= $this->config->item("sina_conf"); } /** * @uses : ΢¼ * @param : * @return : $sina_url----¼ַ */ public function sina_login(){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); $sina_url = $obj->getAuthorizeURL( $this->sina['WB_CALLBACK_URL'] ); return $sina_url; } /** * @uses : ¼ͨصcodeֵȡtokenʵȨɣȻȡûϢ * @param : $code * @return : $user_message--ûϢ */ public function sina_callback($code){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); if (isset($code)) { $keys = array(); $keys['code'] = $code; $keys['redirect_uri'] = $this->sina['WB_CALLBACK_URL']; try { $token = $obj->getAccessToken( 'code', $keys ) ;//Ȩ } catch (OAuthException $e) { } } $c = new SaeTClientV2($this->sina['App_Key'], $this->sina['App_Secret'], $token['access_token']); $ms =$c->home_timeline(); $uid_get = $c->get_uid();//ȡu_id $uid = $uid_get['uid']; $user_message = $c->show_user_by_id($uid);//ȡûϢ return $user_message; } /** * @uses : ѯ¼ * @param : $where * @return : ¼û¼ */ public function select_third($where) { $result = false; $this->db->select(); $this->db->from($this->third); $this->db->where($where); $query = $this->db->get(); if($query){ $result = $query->row_array(); } return $result; } /*- * @uses : sina---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_name($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.sina_id={$where}"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; } /** * @uses : qq---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_qqname($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.qq_id='{$where}'"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; }

/** * @uses : û͵¼Ϣ * @param : $datas * @return : */ public function binding_third($datas) { if (!is_array($datas)) show_error ('wrong param'); if($datas['sina_id']==0 && $datas['qq_id']==0) return; $resa =''; $resb =''; $resa = $this->select_third(array("user_id"=>$datas['user_id'])); $temp =array( "user_id"=>$datas['user_id'], "sina_id"=>$resa['sina_id']!=0 ? $resa['sina_id'] : $datas['sina_id'], "qq_id" => $resa['qq_id']!=0 ? $resa['qq_id'] : $datas['qq_id'], ); if($resa){ $resb = $this->db->update($this->third, $temp,array("user_id"=>$datas['user_id'])); }else{ $resb = $this->db->insert($this->third,$temp); } if($resb) { $this->session->unset_userdata('sina_id');//ע $this->session->unset_userdata('qq_id');//ע } return $resb; }}

˵:codeļcallback.phpģ7ϸ롣ļmodelݱˣǿͼļˡ

5.д¼ application/controllers£login.phpļ(Լȡ)룺

ƴ :<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*** Description of index* @author victory*/class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('login_model','login');//DZĿû¼࣬ṩԭ룬ΪͬĿͬɸĿԼװ $this->load->model("third_login_model","third"); $this->load->library('session'); }

public function index() { header("content-type: text/html; charset=utf-8"); $this->load->model("third_login_model","third");//˵¼ӿ $datas['sina_url'] = $this->third->sina_login();//еsina_login $this->load->view("index.php",$datas);//ȡͼļ

}

public function callback(){ header("content-type: text/html; charset=utf-8"); $this->load->model("user_reg_model","user_reg"); $code = $_REQUEST['code'];//codeֵļcallback.php $arr =array(); $arr = $this->third->sina_callback($code);//ͨȨȡûϢu_id $res = $this->third->select_third(array("sina_id"=>$arr['id'])); if(!empty($res)){//ûʺż¼жʺǷ񼤻 $user_info = $this->user_reg->user_detect(array("id"=>$res['user_id']));//ѯû״̬user_detectDzѯûϢķҲ˵ˣlogin_model.php౾ṩҪԼȥװ if($user_info['status']){//status״̬жûʺǷ񼤻,user_regеֶstatus1Ϊδ0ΪѼ echo "<script>alert('˺δȥ伤');location='/login/index';</script>";die(); } $datas = $this->third->select_user_name($arr['id']);//󣬰Ϣдû͵¼ $uname = $datas['username'];//username,passworduser_regֶΣuser_regݱĹҲṩΪÿĿһҪʵĿ $password = $datas['password']; $this->load->model("login_model","login"); $this->login->validation($uname,$password);//validationǵ¼ҪҪڵ¼ʱ򣬽ûϢд¼ṩд¼Ĵ echo "<script>alert('¼ɹ!');location='/user_center'</script>";die(); }else{//ûûм¼ѯûǷƽ̨йʺţûתעᣬת¼ $this->session->set_userdata('sina_id',$arr['id']); echo "<script>if(!confirm('Ƿƽ̨עû')){location='/register/index'}else{location='/login'};</script>"; } }

public function login_validation(){ //¼ûid ,sina_id,qq_idļ¼ $third_info =array( "user_id" => $user_ser['id'], "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($third_info['sina_id']||$third_info['qq_id']) $this->third->binding_third($third_info); // }//

//עûϢдuser_regͬʱҲsina_idдthird_loginֻչʾ¼ӿûidݱĴclass Register extends CI_Controller {

public function __construct() { parent::__construct(); $this->load->library('session'); } public function reg() { $haha =array( "user_id" => $rs, "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($haha['sina_id']||$haha['qq_id']) $this->third->binding_third($haha); }}

6.ͼļ΢¼ťapplication/view½index.phpļ룺

ƴ :<html><head> <meta content="text/html; charset=utf-8"> <title>΢¼ӿ</title></head><body> <div><a href="<?=$sina_url?>"><img src="http://images.cnblogs.com/weibo_login.png" width="110" /></a></div></body></html>

˵ǸͼƬťͼƬڹأصַhttp://open.weibo.com/widget/loginbutton.php

7.صַǰڵ1ļļʱ˻صַhttp://test.com/callback.php callback.phpʲôطأҪںindex.phpͬλãapplicationҲͬġڿʼĿ¼½ļcallback.php룺

ƴ :<?php

/** To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates* and open the template in the editor.*///΢¼صļ·תƵlogin/callbackcodeֵȥ$code ='';$url = '';$str ='';$code = $_REQUEST['code'];$url = "/login/callback";

$str = "<!doctype html><html> <head> <meta charset=\"UTF-8\"> <title>Զת</title> </head><body>";$str .="<form action=\"{$url}\" method=\"post\" id=\"form\" autocomplete='off'>";$str .="<input type='hidden' name='code' value='{$code}'>";$str .="</form> </body> </html> <script type=\"text/javascript\"> document.getElementById('form').submit(); </script>";echo $str;

ʱindex.phpļʱ򣬻ῴһ΢ʺŵ¼ĵ¼ťťת΢¼ҳ棬Ҫ΢û,ͬIJҲ˵ˡ

CIܿ΢¼ӿԴ

CIܿ΢¼ӿԴCIܿ΢¼ӿԴ

̣ԭ 1.ͨcodeaccess_tokenͨȨȡûϢûu_id(u_idںĵ¼sina_idǸҪԼ) 2.ѯ¼ûsina_id,2һûƽ̨ѾʺţʱҪƽ̨磺ƽ̨ûǣuser_regûid󶨵¼ǣthird_loginȻÿͻ¼; ûƽ̨ûʺţתעҳעᣬעͬʱϢдuer_regͬʱҲûsina_idд¼а; 3.ѯ¼(third_login)ûsina_id,ٲѯû(user_reg)Ѿֱӵ¼ûмʾûȥ伤ʺš

濪ʼ꽲裺һApp keyApp secretַhttp://open.weibo.com/ ҳվWEBȥͺˣͨõApp Key App Secret£App Key1428003339App Sercetf1c6177a38b39f764c76a1690720a6dcصַhttp://test.com/callback.php

˵ʺžDzʺţڿʱʺԣʺ޷¼޷ϢġǰϹ¿̣ҪġֻҪ˼·ˣʣ¾ôʵ˼롣

ڶSDKphpģصַhttp://code.google.com/p/libweibo/downloads/list5ļһsaetv2.ex.class.phpֻҪļ

1.һ¼Ա洢¼Ϣu_id,QQopenidǶΨһģʶûǸ洢

ƴ :CREATE TABLE IF NOT EXISTS `third_login` ( `user_id` INT(6) NOT NULL, `sina_id` BIGINT(16) NULL, `qq_id` varchar(64) NULL, PRIMARY KEY (`user_id`), UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC), INDEX `sina_id` (`sina_id` ASC), INDEX `index4` (`qq_id` ASC))ENGINE = MyISAMDEFAULT CHARACTER SET = utf8COLLATE = utf8_binCOMMENT = '¼'

˵ƽ̨صu_id,ûΨһʶҰΪsina_id,user_idǹƽ̨ûuser_regidģuser_regﲻг԰ʵĿƼIJphpmyadmin,MySQL Workbench,㡣ֻҪ˵¼ӿڣǿ԰qq_idֶȥ

2.дļapplication½һļsina_conf.php,Ѹ뵽App Key App Secretдȥ£

ƴ :<?php$config["sina_conf"] = array( "App_Key" => '1428003339', "App_Secret" =>'f1c6177a38b39f764c76a1690720a6dc', "WB_CALLBACK_URL" => 'http://test.com/callback.php');

3.oauth֤࣬Ѹsaetv2.ex.class.phpļƵapplication/libraries¡˵ǷdzҪ࣬¼ȨȡûϢҪõеķûûȥˣԭⲻճapplication/libraries¡

4.д΢¼ࣨQQ¼ҲãQQ¼Ҳװһˣֻ˵¼ӿڣҲӰ죩application/models½һļthird_login_model.php룺

ƴ :<?php/*** Description of third_login_model*ӿȨ¼model* @author*/class third_login_model extends CI_Model{ //put your code here private $sina=array(); private $qq =array(); private $users =''; private $third=''; public function __construct() { parent::__construct();// $this->l = DIRECTORY_SEPARATOR; $this->load->database(); $this->load->library('session'); include_once APPPATH."/libraries"."/saetv2.ex.class.php"; $this->third = $this->db->'third_login';//¼ $this->users = $this->db->'user_reg';//Ŀû $this->config->load("sina_conf"); $this->sina= $this->config->item("sina_conf"); } /** * @uses : ΢¼ * @param : * @return : $sina_url----¼ַ */ public function sina_login(){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); $sina_url = $obj->getAuthorizeURL( $this->sina['WB_CALLBACK_URL'] ); return $sina_url; } /** * @uses : ¼ͨصcodeֵȡtokenʵȨɣȻȡûϢ * @param : $code * @return : $user_message--ûϢ */ public function sina_callback($code){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); if (isset($code)) { $keys = array(); $keys['code'] = $code; $keys['redirect_uri'] = $this->sina['WB_CALLBACK_URL']; try { $token = $obj->getAccessToken( 'code', $keys ) ;//Ȩ } catch (OAuthException $e) { } } $c = new SaeTClientV2($this->sina['App_Key'], $this->sina['App_Secret'], $token['access_token']); $ms =$c->home_timeline(); $uid_get = $c->get_uid();//ȡu_id $uid = $uid_get['uid']; $user_message = $c->show_user_by_id($uid);//ȡûϢ return $user_message; } /** * @uses : ѯ¼ * @param : $where * @return : ¼û¼ */ public function select_third($where) { $result = false; $this->db->select(); $this->db->from($this->third); $this->db->where($where); $query = $this->db->get(); if($query){ $result = $query->row_array(); } return $result; } /*- * @uses : sina---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_name($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.sina_id={$where}"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; } /** * @uses : qq---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_qqname($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.qq_id='{$where}'"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; }

/** * @uses : û͵¼Ϣ * @param : $datas * @return : */ public function binding_third($datas) { if (!is_array($datas)) show_error ('wrong param'); if($datas['sina_id']==0 && $datas['qq_id']==0) return; $resa =''; $resb =''; $resa = $this->select_third(array("user_id"=>$datas['user_id'])); $temp =array( "user_id"=>$datas['user_id'], "sina_id"=>$resa['sina_id']!=0 ? $resa['sina_id'] : $datas['sina_id'], "qq_id" => $resa['qq_id']!=0 ? $resa['qq_id'] : $datas['qq_id'], ); if($resa){ $resb = $this->db->update($this->third, $temp,array("user_id"=>$datas['user_id'])); }else{ $resb = $this->db->insert($this->third,$temp); } if($resb) { $this->session->unset_userdata('sina_id');//ע $this->session->unset_userdata('qq_id');//ע } return $resb; }}

˵:codeļcallback.phpģ7ϸ롣ļmodelݱˣǿͼļˡ

5.д¼ application/controllers£login.phpļ(Լȡ)룺

ƴ :<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*** Description of index* @author victory*/class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('login_model','login');//DZĿû¼࣬ṩԭ룬ΪͬĿͬɸĿԼװ $this->load->model("third_login_model","third"); $this->load->library('session'); }

public function index() { header("content-type: text/html; charset=utf-8"); $this->load->model("third_login_model","third");//˵¼ӿ $datas['sina_url'] = $this->third->sina_login();//еsina_login $this->load->view("index.php",$datas);//ȡͼļ

}

public function callback(){ header("content-type: text/html; charset=utf-8"); $this->load->model("user_reg_model","user_reg"); $code = $_REQUEST['code'];//codeֵļcallback.php $arr =array(); $arr = $this->third->sina_callback($code);//ͨȨȡûϢu_id $res = $this->third->select_third(array("sina_id"=>$arr['id'])); if(!empty($res)){//ûʺż¼жʺǷ񼤻 $user_info = $this->user_reg->user_detect(array("id"=>$res['user_id']));//ѯû״̬user_detectDzѯûϢķҲ˵ˣlogin_model.php౾ṩҪԼȥװ if($user_info['status']){//status״̬жûʺǷ񼤻,user_regеֶstatus1Ϊδ0ΪѼ echo "<script>alert('˺δȥ伤');location='/login/index';</script>";die(); } $datas = $this->third->select_user_name($arr['id']);//󣬰Ϣдû͵¼ $uname = $datas['username'];//username,passworduser_regֶΣuser_regݱĹҲṩΪÿĿһҪʵĿ $password = $datas['password']; $this->load->model("login_model","login"); $this->login->validation($uname,$password);//validationǵ¼ҪҪڵ¼ʱ򣬽ûϢд¼ṩд¼Ĵ echo "<script>alert('¼ɹ!');location='/user_center'</script>";die(); }else{//ûûм¼ѯûǷƽ̨йʺţûתעᣬת¼ $this->session->set_userdata('sina_id',$arr['id']); echo "<script>if(!confirm('Ƿƽ̨עû')){location='/register/index'}else{location='/login'};</script>"; } }

public function login_validation(){ //¼ûid ,sina_id,qq_idļ¼ $third_info =array( "user_id" => $user_ser['id'], "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($third_info['sina_id']||$third_info['qq_id']) $this->third->binding_third($third_info); // }//

//עûϢдuser_regͬʱҲsina_idдthird_loginֻչʾ¼ӿûidݱĴclass Register extends CI_Controller {

public function __construct() { parent::__construct(); $this->load->library('session'); } public function reg() { $haha =array( "user_id" => $rs, "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($haha['sina_id']||$haha['qq_id']) $this->third->binding_third($haha); }}

6.ͼļ΢¼ťapplication/view½index.phpļ룺

ƴ :<html><head> <meta content="text/html; charset=utf-8"> <title>΢¼ӿ</title></head><body> <div><a href="<?=$sina_url?>"><img src="http://images.cnblogs.com/weibo_login.png" width="110" /></a></div></body></html>

˵ǸͼƬťͼƬڹأصַhttp://open.weibo.com/widget/loginbutton.php

7.صַǰڵ1ļļʱ˻صַhttp://test.com/callback.php callback.phpʲôطأҪںindex.phpͬλãapplicationҲͬġڿʼĿ¼½ļcallback.php룺

ƴ :<?php

/** To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates* and open the template in the editor.*///΢¼صļ·תƵlogin/callbackcodeֵȥ$code ='';$url = '';$str ='';$code = $_REQUEST['code'];$url = "/login/callback";

$str = "<!doctype html><html> <head> <meta charset=\"UTF-8\"> <title>Զת</title> </head><body>";$str .="<form action=\"{$url}\" method=\"post\" id=\"form\" autocomplete='off'>";$str .="<input type='hidden' name='code' value='{$code}'>";$str .="</form> </body> </html> <script type=\"text/javascript\"> document.getElementById('form').submit(); </script>";echo $str;

ʱindex.phpļʱ򣬻ῴһ΢ʺŵ¼ĵ¼ťťת΢¼ҳ棬Ҫ΢û,ͬIJҲ˵ˡ

ӯѹʱ

̣ԭ 1.ͨcodeaccess_tokenͨȨȡûϢûu_id(u_idںĵ¼sina_idǸҪԼ) 2.ѯ¼ûsina_id,2һûƽ̨ѾʺţʱҪƽ̨磺ƽ̨ûǣuser_regûid󶨵¼ǣthird_loginȻÿͻ¼; ûƽ̨ûʺţתעҳעᣬעͬʱϢдuer_regͬʱҲûsina_idд¼а; 3.ѯ¼(third_login)ûsina_id,ٲѯû(user_reg)Ѿֱӵ¼ûмʾûȥ伤ʺš

濪ʼ꽲裺һApp keyApp secretַhttp://open.weibo.com/ ҳվWEBȥͺˣͨõApp Key App Secret£App Key1428003339App Sercetf1c6177a38b39f764c76a1690720a6dcصַhttp://test.com/callback.php

˵ʺžDzʺţڿʱʺԣʺ޷¼޷ϢġǰϹ¿̣ҪġֻҪ˼·ˣʣ¾ôʵ˼롣

ڶSDKphpģصַhttp://code.google.com/p/libweibo/downloads/list5ļһsaetv2.ex.class.phpֻҪļ

1.һ¼Ա洢¼Ϣu_id,QQopenidǶΨһģʶûǸ洢

ƴ :CREATE TABLE IF NOT EXISTS `third_login` ( `user_id` INT(6) NOT NULL, `sina_id` BIGINT(16) NULL, `qq_id` varchar(64) NULL, PRIMARY KEY (`user_id`), UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC), INDEX `sina_id` (`sina_id` ASC), INDEX `index4` (`qq_id` ASC))ENGINE = MyISAMDEFAULT CHARACTER SET = utf8COLLATE = utf8_binCOMMENT = '¼'

˵ƽ̨صu_id,ûΨһʶҰΪsina_id,user_idǹƽ̨ûuser_regidģuser_regﲻг԰ʵĿƼIJphpmyadmin,MySQL Workbench,㡣ֻҪ˵¼ӿڣǿ԰qq_idֶȥ

2.дļapplication½һļsina_conf.php,Ѹ뵽App Key App Secretдȥ£

ƴ :<?php$config["sina_conf"] = array( "App_Key" => '1428003339', "App_Secret" =>'f1c6177a38b39f764c76a1690720a6dc', "WB_CALLBACK_URL" => 'http://test.com/callback.php');

3.oauth֤࣬Ѹsaetv2.ex.class.phpļƵapplication/libraries¡˵ǷdzҪ࣬¼ȨȡûϢҪõеķûûȥˣԭⲻճapplication/libraries¡

4.д΢¼ࣨQQ¼ҲãQQ¼Ҳװһˣֻ˵¼ӿڣҲӰ죩application/models½һļthird_login_model.php룺

ƴ :<?php/*** Description of third_login_model*ӿȨ¼model* @author*/class third_login_model extends CI_Model{ //put your code here private $sina=array(); private $qq =array(); private $users =''; private $third=''; public function __construct() { parent::__construct();// $this->l = DIRECTORY_SEPARATOR; $this->load->database(); $this->load->library('session'); include_once APPPATH."/libraries"."/saetv2.ex.class.php"; $this->third = $this->db->'third_login';//¼ $this->users = $this->db->'user_reg';//Ŀû $this->config->load("sina_conf"); $this->sina= $this->config->item("sina_conf"); } /** * @uses : ΢¼ * @param : * @return : $sina_url----¼ַ */ public function sina_login(){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); $sina_url = $obj->getAuthorizeURL( $this->sina['WB_CALLBACK_URL'] ); return $sina_url; } /** * @uses : ¼ͨصcodeֵȡtokenʵȨɣȻȡûϢ * @param : $code * @return : $user_message--ûϢ */ public function sina_callback($code){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); if (isset($code)) { $keys = array(); $keys['code'] = $code; $keys['redirect_uri'] = $this->sina['WB_CALLBACK_URL']; try { $token = $obj->getAccessToken( 'code', $keys ) ;//Ȩ } catch (OAuthException $e) { } } $c = new SaeTClientV2($this->sina['App_Key'], $this->sina['App_Secret'], $token['access_token']); $ms =$c->home_timeline(); $uid_get = $c->get_uid();//ȡu_id $uid = $uid_get['uid']; $user_message = $c->show_user_by_id($uid);//ȡûϢ return $user_message; } /** * @uses : ѯ¼ * @param : $where * @return : ¼û¼ */ public function select_third($where) { $result = false; $this->db->select(); $this->db->from($this->third); $this->db->where($where); $query = $this->db->get(); if($query){ $result = $query->row_array(); } return $result; } /*- * @uses : sina---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_name($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.sina_id={$where}"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; } /** * @uses : qq---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_qqname($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.qq_id='{$where}'"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; }

/** * @uses : û͵¼Ϣ * @param : $datas * @return : */ public function binding_third($datas) { if (!is_array($datas)) show_error ('wrong param'); if($datas['sina_id']==0 && $datas['qq_id']==0) return; $resa =''; $resb =''; $resa = $this->select_third(array("user_id"=>$datas['user_id'])); $temp =array( "user_id"=>$datas['user_id'], "sina_id"=>$resa['sina_id']!=0 ? $resa['sina_id'] : $datas['sina_id'], "qq_id" => $resa['qq_id']!=0 ? $resa['qq_id'] : $datas['qq_id'], ); if($resa){ $resb = $this->db->update($this->third, $temp,array("user_id"=>$datas['user_id'])); }else{ $resb = $this->db->insert($this->third,$temp); } if($resb) { $this->session->unset_userdata('sina_id');//ע $this->session->unset_userdata('qq_id');//ע } return $resb; }}

˵:codeļcallback.phpģ7ϸ롣ļmodelݱˣǿͼļˡ

5.д¼ application/controllers£login.phpļ(Լȡ)룺

ƴ :<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*** Description of index* @author victory*/class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('login_model','login');//DZĿû¼࣬ṩԭ룬ΪͬĿͬɸĿԼװ $this->load->model("third_login_model","third"); $this->load->library('session'); }

public function index() { header("content-type: text/html; charset=utf-8"); $this->load->model("third_login_model","third");//˵¼ӿ $datas['sina_url'] = $this->third->sina_login();//еsina_login $this->load->view("index.php",$datas);//ȡͼļ

}

public function callback(){ header("content-type: text/html; charset=utf-8"); $this->load->model("user_reg_model","user_reg"); $code = $_REQUEST['code'];//codeֵļcallback.php $arr =array(); $arr = $this->third->sina_callback($code);//ͨȨȡûϢu_id $res = $this->third->select_third(array("sina_id"=>$arr['id'])); if(!empty($res)){//ûʺż¼жʺǷ񼤻 $user_info = $this->user_reg->user_detect(array("id"=>$res['user_id']));//ѯû״̬user_detectDzѯûϢķҲ˵ˣlogin_model.php౾ṩҪԼȥװ if($user_info['status']){//status״̬жûʺǷ񼤻,user_regеֶstatus1Ϊδ0ΪѼ echo "<script>alert('˺δȥ伤');location='/login/index';</script>";die(); } $datas = $this->third->select_user_name($arr['id']);//󣬰Ϣдû͵¼ $uname = $datas['username'];//username,passworduser_regֶΣuser_regݱĹҲṩΪÿĿһҪʵĿ $password = $datas['password']; $this->load->model("login_model","login"); $this->login->validation($uname,$password);//validationǵ¼ҪҪڵ¼ʱ򣬽ûϢд¼ṩд¼Ĵ echo "<script>alert('¼ɹ!');location='/user_center'</script>";die(); }else{//ûûм¼ѯûǷƽ̨йʺţûתעᣬת¼ $this->session->set_userdata('sina_id',$arr['id']); echo "<script>if(!confirm('Ƿƽ̨עû')){location='/register/index'}else{location='/login'};</script>"; } }

public function login_validation(){ //¼ûid ,sina_id,qq_idļ¼ $third_info =array( "user_id" => $user_ser['id'], "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($third_info['sina_id']||$third_info['qq_id']) $this->third->binding_third($third_info); // }//

//עûϢдuser_regͬʱҲsina_idдthird_loginֻչʾ¼ӿûidݱĴclass Register extends CI_Controller {

public function __construct() { parent::__construct(); $this->load->library('session'); } public function reg() { $haha =array( "user_id" => $rs, "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($haha['sina_id']||$haha['qq_id']) $this->third->binding_third($haha); }}

6.ͼļ΢¼ťapplication/view½index.phpļ룺

ƴ :<html><head> <meta content="text/html; charset=utf-8"> <title>΢¼ӿ</title></head><body> <div><a href="<?=$sina_url?>"><img src="http://images.cnblogs.com/weibo_login.png" width="110" /></a></div></body></html>

˵ǸͼƬťͼƬڹأصַhttp://open.weibo.com/widget/loginbutton.php

7.صַǰڵ1ļļʱ˻صַhttp://test.com/callback.php callback.phpʲôطأҪںindex.phpͬλãapplicationҲͬġڿʼĿ¼½ļcallback.php룺

ƴ :<?php

/** To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates* and open the template in the editor.*///΢¼صļ·תƵlogin/callbackcodeֵȥ$code ='';$url = '';$str ='';$code = $_REQUEST['code'];$url = "/login/callback";

$str = "<!doctype html><html> <head> <meta charset=\"UTF-8\"> <title>Զת</title> </head><body>";$str .="<form action=\"{$url}\" method=\"post\" id=\"form\" autocomplete='off'>";$str .="<input type='hidden' name='code' value='{$code}'>";$str .="</form> </body> </html> <script type=\"text/javascript\"> document.getElementById('form').submit(); </script>";echo $str;

ʱindex.phpļʱ򣬻ῴһ΢ʺŵ¼ĵ¼ťťת΢¼ҳ棬Ҫ΢û,ͬIJҲ˵ˡ

ӯѹʱ

̣ԭ 1.ͨcodeaccess_tokenͨȨȡûϢûu_id(u_idںĵ¼sina_idǸҪԼ) 2.ѯ¼ûsina_id,2һûƽ̨ѾʺţʱҪƽ̨磺ƽ̨ûǣuser_regûid󶨵¼ǣthird_loginȻÿͻ¼; ûƽ̨ûʺţתעҳעᣬעͬʱϢдuer_regͬʱҲûsina_idд¼а; 3.ѯ¼(third_login)ûsina_id,ٲѯû(user_reg)Ѿֱӵ¼ûмʾûȥ伤ʺš

濪ʼ꽲裺һApp keyApp secretַhttp://open.weibo.com/ ҳվWEBȥͺˣͨõApp Key App Secret£App Key1428003339App Sercetf1c6177a38b39f764c76a1690720a6dcصַhttp://test.com/callback.php

˵ʺžDzʺţڿʱʺԣʺ޷¼޷ϢġǰϹ¿̣ҪġֻҪ˼·ˣʣ¾ôʵ˼롣

ڶSDKphpģصַhttp://code.google.com/p/libweibo/downloads/list5ļһsaetv2.ex.class.phpֻҪļ

1.һ¼Ա洢¼Ϣu_id,QQopenidǶΨһģʶûǸ洢

ƴ :CREATE TABLE IF NOT EXISTS `third_login` ( `user_id` INT(6) NOT NULL, `sina_id` BIGINT(16) NULL, `qq_id` varchar(64) NULL, PRIMARY KEY (`user_id`), UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC), INDEX `sina_id` (`sina_id` ASC), INDEX `index4` (`qq_id` ASC))ENGINE = MyISAMDEFAULT CHARACTER SET = utf8COLLATE = utf8_binCOMMENT = '¼'

˵ƽ̨صu_id,ûΨһʶҰΪsina_id,user_idǹƽ̨ûuser_regidģuser_regﲻг԰ʵĿƼIJphpmyadmin,MySQL Workbench,㡣ֻҪ˵¼ӿڣǿ԰qq_idֶȥ

2.дļapplication½һļsina_conf.php,Ѹ뵽App Key App Secretдȥ£

ƴ :<?php$config["sina_conf"] = array( "App_Key" => '1428003339', "App_Secret" =>'f1c6177a38b39f764c76a1690720a6dc', "WB_CALLBACK_URL" => 'http://test.com/callback.php');

3.oauth֤࣬Ѹsaetv2.ex.class.phpļƵapplication/libraries¡˵ǷdzҪ࣬¼ȨȡûϢҪõеķûûȥˣԭⲻճapplication/libraries¡

4.д΢¼ࣨQQ¼ҲãQQ¼Ҳװһˣֻ˵¼ӿڣҲӰ죩application/models½һļthird_login_model.php룺

ƴ :<?php/*** Description of third_login_model*ӿȨ¼model* @author*/class third_login_model extends CI_Model{ //put your code here private $sina=array(); private $qq =array(); private $users =''; private $third=''; public function __construct() { parent::__construct();// $this->l = DIRECTORY_SEPARATOR; $this->load->database(); $this->load->library('session'); include_once APPPATH."/libraries"."/saetv2.ex.class.php"; $this->third = $this->db->'third_login';//¼ $this->users = $this->db->'user_reg';//Ŀû $this->config->load("sina_conf"); $this->sina= $this->config->item("sina_conf"); } /** * @uses : ΢¼ * @param : * @return : $sina_url----¼ַ */ public function sina_login(){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); $sina_url = $obj->getAuthorizeURL( $this->sina['WB_CALLBACK_URL'] ); return $sina_url; } /** * @uses : ¼ͨصcodeֵȡtokenʵȨɣȻȡûϢ * @param : $code * @return : $user_message--ûϢ */ public function sina_callback($code){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); if (isset($code)) { $keys = array(); $keys['code'] = $code; $keys['redirect_uri'] = $this->sina['WB_CALLBACK_URL']; try { $token = $obj->getAccessToken( 'code', $keys ) ;//Ȩ } catch (OAuthException $e) { } } $c = new SaeTClientV2($this->sina['App_Key'], $this->sina['App_Secret'], $token['access_token']); $ms =$c->home_timeline(); $uid_get = $c->get_uid();//ȡu_id $uid = $uid_get['uid']; $user_message = $c->show_user_by_id($uid);//ȡûϢ return $user_message; } /** * @uses : ѯ¼ * @param : $where * @return : ¼û¼ */ public function select_third($where) { $result = false; $this->db->select(); $this->db->from($this->third); $this->db->where($where); $query = $this->db->get(); if($query){ $result = $query->row_array(); } return $result; } /*- * @uses : sina---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_name($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.sina_id={$where}"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; } /** * @uses : qq---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_qqname($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.qq_id='{$where}'"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; }

/** * @uses : û͵¼Ϣ * @param : $datas * @return : */ public function binding_third($datas) { if (!is_array($datas)) show_error ('wrong param'); if($datas['sina_id']==0 && $datas['qq_id']==0) return; $resa =''; $resb =''; $resa = $this->select_third(array("user_id"=>$datas['user_id'])); $temp =array( "user_id"=>$datas['user_id'], "sina_id"=>$resa['sina_id']!=0 ? $resa['sina_id'] : $datas['sina_id'], "qq_id" => $resa['qq_id']!=0 ? $resa['qq_id'] : $datas['qq_id'], ); if($resa){ $resb = $this->db->update($this->third, $temp,array("user_id"=>$datas['user_id'])); }else{ $resb = $this->db->insert($this->third,$temp); } if($resb) { $this->session->unset_userdata('sina_id');//ע $this->session->unset_userdata('qq_id');//ע } return $resb; }}

˵:codeļcallback.phpģ7ϸ롣ļmodelݱˣǿͼļˡ

5.д¼ application/controllers£login.phpļ(Լȡ)룺

ƴ :<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*** Description of index* @author victory*/class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('login_model','login');//DZĿû¼࣬ṩԭ룬ΪͬĿͬɸĿԼװ $this->load->model("third_login_model","third"); $this->load->library('session'); }

public function index() { header("content-type: text/html; charset=utf-8"); $this->load->model("third_login_model","third");//˵¼ӿ $datas['sina_url'] = $this->third->sina_login();//еsina_login $this->load->view("index.php",$datas);//ȡͼļ

}

public function callback(){ header("content-type: text/html; charset=utf-8"); $this->load->model("user_reg_model","user_reg"); $code = $_REQUEST['code'];//codeֵļcallback.php $arr =array(); $arr = $this->third->sina_callback($code);//ͨȨȡûϢu_id $res = $this->third->select_third(array("sina_id"=>$arr['id'])); if(!empty($res)){//ûʺż¼жʺǷ񼤻 $user_info = $this->user_reg->user_detect(array("id"=>$res['user_id']));//ѯû״̬user_detectDzѯûϢķҲ˵ˣlogin_model.php౾ṩҪԼȥװ if($user_info['status']){//status״̬жûʺǷ񼤻,user_regеֶstatus1Ϊδ0ΪѼ echo "<script>alert('˺δȥ伤');location='/login/index';</script>";die(); } $datas = $this->third->select_user_name($arr['id']);//󣬰Ϣдû͵¼ $uname = $datas['username'];//username,passworduser_regֶΣuser_regݱĹҲṩΪÿĿһҪʵĿ $password = $datas['password']; $this->load->model("login_model","login"); $this->login->validation($uname,$password);//validationǵ¼ҪҪڵ¼ʱ򣬽ûϢд¼ṩд¼Ĵ echo "<script>alert('¼ɹ!');location='/user_center'</script>";die(); }else{//ûûм¼ѯûǷƽ̨йʺţûתעᣬת¼ $this->session->set_userdata('sina_id',$arr['id']); echo "<script>if(!confirm('Ƿƽ̨עû')){location='/register/index'}else{location='/login'};</script>"; } }

public function login_validation(){ //¼ûid ,sina_id,qq_idļ¼ $third_info =array( "user_id" => $user_ser['id'], "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($third_info['sina_id']||$third_info['qq_id']) $this->third->binding_third($third_info); // }//

//עûϢдuser_regͬʱҲsina_idдthird_loginֻչʾ¼ӿûidݱĴclass Register extends CI_Controller {

public function __construct() { parent::__construct(); $this->load->library('session'); } public function reg() { $haha =array( "user_id" => $rs, "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($haha['sina_id']||$haha['qq_id']) $this->third->binding_third($haha); }}

6.ͼļ΢¼ťapplication/view½index.phpļ룺

ƴ :<html><head> <meta content="text/html; charset=utf-8"> <title>΢¼ӿ</title></head><body> <div><a href="<?=$sina_url?>"><img src="http://images.cnblogs.com/weibo_login.png" width="110" /></a></div></body></html>

˵ǸͼƬťͼƬڹأصַhttp://open.weibo.com/widget/loginbutton.php

7.صַǰڵ1ļļʱ˻صַhttp://test.com/callback.php callback.phpʲôطأҪںindex.phpͬλãapplicationҲͬġڿʼĿ¼½ļcallback.php룺

ƴ :<?php

/** To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates* and open the template in the editor.*///΢¼صļ·תƵlogin/callbackcodeֵȥ$code ='';$url = '';$str ='';$code = $_REQUEST['code'];$url = "/login/callback";

$str = "<!doctype html><html> <head> <meta charset=\"UTF-8\"> <title>Զת</title> </head><body>";$str .="<form action=\"{$url}\" method=\"post\" id=\"form\" autocomplete='off'>";$str .="<input type='hidden' name='code' value='{$code}'>";$str .="</form> </body> </html> <script type=\"text/javascript\"> document.getElementById('form').submit(); </script>";echo $str;

ʱindex.phpļʱ򣬻ῴһ΢ʺŵ¼ĵ¼ťťת΢¼ҳ棬Ҫ΢û,ͬIJҲ˵ˡ

CIܿ΢¼ӿԴCIܿ΢¼ӿԴ

̣ԭ 1.ͨcodeaccess_tokenͨȨȡûϢûu_id(u_idںĵ¼sina_idǸҪԼ) 2.ѯ¼ûsina_id,2һûƽ̨ѾʺţʱҪƽ̨磺ƽ̨ûǣuser_regûid󶨵¼ǣthird_loginȻÿͻ¼; ûƽ̨ûʺţתעҳעᣬעͬʱϢдuer_regͬʱҲûsina_idд¼а; 3.ѯ¼(third_login)ûsina_id,ٲѯû(user_reg)Ѿֱӵ¼ûмʾûȥ伤ʺš

濪ʼ꽲裺һApp keyApp secretַhttp://open.weibo.com/ ҳվWEBȥͺˣͨõApp Key App Secret£App Key1428003339App Sercetf1c6177a38b39f764c76a1690720a6dcصַhttp://test.com/callback.php

˵ʺžDzʺţڿʱʺԣʺ޷¼޷ϢġǰϹ¿̣ҪġֻҪ˼·ˣʣ¾ôʵ˼롣

ڶSDKphpģصַhttp://code.google.com/p/libweibo/downloads/list5ļһsaetv2.ex.class.phpֻҪļ

1.һ¼Ա洢¼Ϣu_id,QQopenidǶΨһģʶûǸ洢

ƴ :CREATE TABLE IF NOT EXISTS `third_login` ( `user_id` INT(6) NOT NULL, `sina_id` BIGINT(16) NULL, `qq_id` varchar(64) NULL, PRIMARY KEY (`user_id`), UNIQUE INDEX `user_id_UNIQUE` (`user_id` ASC), INDEX `sina_id` (`sina_id` ASC), INDEX `index4` (`qq_id` ASC))ENGINE = MyISAMDEFAULT CHARACTER SET = utf8COLLATE = utf8_binCOMMENT = '¼'

˵ƽ̨صu_id,ûΨһʶҰΪsina_id,user_idǹƽ̨ûuser_regidģuser_regﲻг԰ʵĿƼIJphpmyadmin,MySQL Workbench,㡣ֻҪ˵¼ӿڣǿ԰qq_idֶȥ

2.дļapplication½һļsina_conf.php,Ѹ뵽App Key App Secretдȥ£

ƴ :<?php$config["sina_conf"] = array( "App_Key" => '1428003339', "App_Secret" =>'f1c6177a38b39f764c76a1690720a6dc', "WB_CALLBACK_URL" => 'http://test.com/callback.php');

3.oauth֤࣬Ѹsaetv2.ex.class.phpļƵapplication/libraries¡˵ǷdzҪ࣬¼ȨȡûϢҪõеķûûȥˣԭⲻճapplication/libraries¡

4.д΢¼ࣨQQ¼ҲãQQ¼Ҳװһˣֻ˵¼ӿڣҲӰ죩application/models½һļthird_login_model.php룺

ƴ :<?php/*** Description of third_login_model*ӿȨ¼model* @author*/class third_login_model extends CI_Model{ //put your code here private $sina=array(); private $qq =array(); private $users =''; private $third=''; public function __construct() { parent::__construct();// $this->l = DIRECTORY_SEPARATOR; $this->load->database(); $this->load->library('session'); include_once APPPATH."/libraries"."/saetv2.ex.class.php"; $this->third = $this->db->'third_login';//¼ $this->users = $this->db->'user_reg';//Ŀû $this->config->load("sina_conf"); $this->sina= $this->config->item("sina_conf"); } /** * @uses : ΢¼ * @param : * @return : $sina_url----¼ַ */ public function sina_login(){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); $sina_url = $obj->getAuthorizeURL( $this->sina['WB_CALLBACK_URL'] ); return $sina_url; } /** * @uses : ¼ͨصcodeֵȡtokenʵȨɣȻȡûϢ * @param : $code * @return : $user_message--ûϢ */ public function sina_callback($code){ $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']); if (isset($code)) { $keys = array(); $keys['code'] = $code; $keys['redirect_uri'] = $this->sina['WB_CALLBACK_URL']; try { $token = $obj->getAccessToken( 'code', $keys ) ;//Ȩ } catch (OAuthException $e) { } } $c = new SaeTClientV2($this->sina['App_Key'], $this->sina['App_Secret'], $token['access_token']); $ms =$c->home_timeline(); $uid_get = $c->get_uid();//ȡu_id $uid = $uid_get['uid']; $user_message = $c->show_user_by_id($uid);//ȡûϢ return $user_message; } /** * @uses : ѯ¼ * @param : $where * @return : ¼û¼ */ public function select_third($where) { $result = false; $this->db->select(); $this->db->from($this->third); $this->db->where($where); $query = $this->db->get(); if($query){ $result = $query->row_array(); } return $result; } /*- * @uses : sina---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_name($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.sina_id={$where}"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; } /** * @uses : qq---ѯû͵¼ * @param : $where * @return : ¼û¼ */ public function select_user_qqname($where) { $field ="user.id,user.password,user.username,utl.*"; $sql = "select {$field} from {$this->third} as utl " ." left join {$this->users} as user on user.id=utl.user_id" . " where utl.qq_id='{$where}'"; $query = $this->db->query($sql); $result = $query->row_array(); return $result; }

/** * @uses : û͵¼Ϣ * @param : $datas * @return : */ public function binding_third($datas) { if (!is_array($datas)) show_error ('wrong param'); if($datas['sina_id']==0 && $datas['qq_id']==0) return; $resa =''; $resb =''; $resa = $this->select_third(array("user_id"=>$datas['user_id'])); $temp =array( "user_id"=>$datas['user_id'], "sina_id"=>$resa['sina_id']!=0 ? $resa['sina_id'] : $datas['sina_id'], "qq_id" => $resa['qq_id']!=0 ? $resa['qq_id'] : $datas['qq_id'], ); if($resa){ $resb = $this->db->update($this->third, $temp,array("user_id"=>$datas['user_id'])); }else{ $resb = $this->db->insert($this->third,$temp); } if($resb) { $this->session->unset_userdata('sina_id');//ע $this->session->unset_userdata('qq_id');//ע } return $resb; }}

˵:codeļcallback.phpģ7ϸ롣ļmodelݱˣǿͼļˡ

5.д¼ application/controllers£login.phpļ(Լȡ)룺

ƴ :<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/*** Description of index* @author victory*/class Login extends CI_Controller { public function __construct() { parent::__construct(); $this->load->model('login_model','login');//DZĿû¼࣬ṩԭ룬ΪͬĿͬɸĿԼװ $this->load->model("third_login_model","third"); $this->load->library('session'); }

public function index() { header("content-type: text/html; charset=utf-8"); $this->load->model("third_login_model","third");//˵¼ӿ $datas['sina_url'] = $this->third->sina_login();//еsina_login $this->load->view("index.php",$datas);//ȡͼļ

}

public function callback(){ header("content-type: text/html; charset=utf-8"); $this->load->model("user_reg_model","user_reg"); $code = $_REQUEST['code'];//codeֵļcallback.php $arr =array(); $arr = $this->third->sina_callback($code);//ͨȨȡûϢu_id $res = $this->third->select_third(array("sina_id"=>$arr['id'])); if(!empty($res)){//ûʺż¼жʺǷ񼤻 $user_info = $this->user_reg->user_detect(array("id"=>$res['user_id']));//ѯû״̬user_detectDzѯûϢķҲ˵ˣlogin_model.php౾ṩҪԼȥװ if($user_info['status']){//status״̬жûʺǷ񼤻,user_regеֶstatus1Ϊδ0ΪѼ echo "<script>alert('˺δȥ伤');location='/login/index';</script>";die(); } $datas = $this->third->select_user_name($arr['id']);//󣬰Ϣдû͵¼ $uname = $datas['username'];//username,passworduser_regֶΣuser_regݱĹҲṩΪÿĿһҪʵĿ $password = $datas['password']; $this->load->model("login_model","login"); $this->login->validation($uname,$password);//validationǵ¼ҪҪڵ¼ʱ򣬽ûϢд¼ṩд¼Ĵ echo "<script>alert('¼ɹ!');location='/user_center'</script>";die(); }else{//ûûм¼ѯûǷƽ̨йʺţûתעᣬת¼ $this->session->set_userdata('sina_id',$arr['id']); echo "<script>if(!confirm('Ƿƽ̨עû')){location='/register/index'}else{location='/login'};</script>"; } }

public function login_validation(){ //¼ûid ,sina_id,qq_idļ¼ $third_info =array( "user_id" => $user_ser['id'], "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($third_info['sina_id']||$third_info['qq_id']) $this->third->binding_third($third_info); // }//

//עûϢдuser_regͬʱҲsina_idдthird_loginֻչʾ¼ӿûidݱĴclass Register extends CI_Controller {

public function __construct() { parent::__construct(); $this->load->library('session'); } public function reg() { $haha =array( "user_id" => $rs, "sina_id" => $this->session->userdata('sina_id'), "qq_id" =>$this->session->userdata('qq_id'), ); if($haha['sina_id']||$haha['qq_id']) $this->third->binding_third($haha); }}

6.ͼļ΢¼ťapplication/view½index.phpļ룺

ƴ :<html><head> <meta content="text/html; charset=utf-8"> <title>΢¼ӿ</title></head><body> <div><a href="<?=$sina_url?>"><img src="http://images.cnblogs.com/weibo_login.png" width="110" /></a></div></body></html>

˵ǸͼƬťͼƬڹأصַhttp://open.weibo.com/widget/loginbutton.php

7.صַǰڵ1ļļʱ˻صַhttp://test.com/callback.php callback.phpʲôطأҪںindex.phpͬλãapplicationҲͬġڿʼĿ¼½ļcallback.php룺

ƴ :<?php

/** To change this license header, choose License Headers in Project Properties.* To change this template file, choose Tools | Templates* and open the template in the editor.*///΢¼صļ·תƵlogin/callbackcodeֵȥ$code ='';$url = '';$str ='';$code = $_REQUEST['code'];$url = "/login/callback";

$str = "<!doctype html><html> <head> <meta charset=\"UTF-8\"> <title>Զת</title> </head><body>";$str .="<form action=\"{$url}\" method=\"post\" id=\"form\" autocomplete='off'>";$str .="<input type='hidden' name='code' value='{$code}'>";$str .="</form> </body> </html> <script type=\"text/javascript\"> document.getElementById('form').submit(); </script>";echo $str;

ʱindex.phpļʱ򣬻ῴһ΢ʺŵ¼ĵ¼ťťת΢¼ҳ棬Ҫ΢û,ͬIJҲ˵ˡ

CIܿ΢¼ӿԴCIܿ΢¼ӿԴӯѹʱ

һƪ

һƪ