Home >Backend Development >PHP Tutorial >CI framework development Sina Weibo login interface full version source code_PHP tutorial
First let’s look at the process:
Principle of the process:
1. Obtain the access_token through code and obtain authorization, and obtain the user’s information (including user u_id) (this u_id is called sina_id in the third-party login table later) , that table needs to be built by yourself)
2. Query the third-party login table. If the user sina_id does not exist, there are two situations. One: the user already has an account on the platform. In this case, the platform (for example: the platform The user table is: user_reg) The user id is bound to the third-party login table (for example: third_login table), and then the customer is allowed to log in; , the information is written into the uer_reg table, and the user sina_id is also written into the third-party login table for binding;
3. Query the third-party login table (third_login), if the user sina_id exists, then query the user table (user_reg), if If the email address has been activated, just log in directly. If it is not activated, the user will be prompted to go to the email address to activate the account.
Step 1: Apply for App key and App secret. Application address: http://open.weibo.com/ Click on the website to access the WEB, go in and apply, and pass You will get the App Key and App Secret as follows:
App Key: 1428003339
App Sercet: f1c6177a38b39f764c76a1690720a6dc
Callback address: http://test.com/callback.php
Step 2: Download the SDK, download the php version, download address (official website): http://code.google.com/p/libweibo/downloads/list, there are 5 files downloaded, one of which is saetv2 .ex.class.php, I only need this file.
Step 3: Code
1
. Create a third-party login table to store third-party login information (Sina is u_id, QQ is openid, they are both unique and used to identify users, We store it based on this):
If you only need to create the Sina login interface, you can remove the qq_id field.
Write the configuration file, create a new file sina_conf.php under application, and write the App Key and App Secret you just applied for . The code is as follows:
3.
oauth authentication class, copy the saetv2.ex.class.php file you just downloaded to application/libraries. Note: This is a very important class. Login, authorization, and obtaining user information all use the methods in this class. Without it, you can’t play. Stick it intact under application/libraries
.
Write a Sina Weibo login class (QQ login is also available, and the QQ login here is also packaged together. Even if I only make the Sina login interface, it will not affect it), and build one under application/models File third_login_model.php, code:
/**
* @uses: Bind user and third-party login form information
* @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文件(名字你可以自己取),代码:
/**
* Description of index
* @author victory
*/
class Login extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('login_model','login');//这个类是本项目的用户登录类,本贴不提供原代码,因为不同的项目,需求不同,可根据你项目需求可以自己封装
$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_detect方法就是查询用户信息的方法,上面也说了,login_model.php这个类本贴不提供,需要大家自己去封装。
if($user_info['status']){//根据status的状态判断用户帐号是否激活,user_reg表中的字段status,1为未激活,0为已激活
echo "<script>alert('您的账号未激活,请去邮箱激活!');location='/login/index';</script>";die();
}
$datas = $this->third->select_user_name($arr['id']);//激活后,把信息写入用户表和第三方登录表
$uname = $datas['username'];//username,password都是user_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(){
//Record additions and modifications of third-party login user id, sina_id, qq_id
$third_info =array(
"user_id" => $user_ser['id' ;, ; >}
//Save
//In the registration controller, user information is written into the user_reg table, and sina_id is also written into the third_login table. Here I only show the code for storing the third-party login interface user id in the data table
class Register extends CI_Controller {
parent::__construct();
$this->load->library('session');
}
id'),
" qq_id" =>$this->session->userdata('qq_id'),
->third->binding_third($haha);
}
}
Save
6.
View file layout Sina Weibo login button, create index.php file
under application/view, code: