控制器:
public function login()
{
if($this->session->userdata('loggedIn') == true)
{
redirect('profile');
}
if(isset($_GET['code']))
{
if($this->google->getAuthenticate())
{
$gpInfo = $this->google->getUserInfo();
$userDatas['oauth_provider'] = 'google';
$userDatas['oauth_uid'] = $gpInfo['id'];
$userDatas['name'] = $gpInfo['given_name'].' '.$gpInfo['family_name'];
$userDatas['email'] = $gpInfo['email'];
$userDatas['user_image'] = !empty($gpInfo['picture'])?$gpInfo['picture']:'';
$userID = $this->Google_user->checkUser($userDatas);
$this->session->set_userdata('loggedIn', true);
$this->session->set_userdata('userDatas', $userDatas);
redirect('profile');
}
}
$data['loginURL'] = $this->google->loginURL();
$this->load->view('login',$data);
}
public function profile()
{
if(!$this->session->userdata('loggedIn')){
redirect(base_url(),'refresh');
}
$data['userDatas'] = $this->session->userdata('userDatas');
$this->load->view('profile',$data);
}
视图:login.php
<a href="<?php echo $loginURL; ?>">Google Login</a>
型号:Google_user.php
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Google_user extends CI_Model {
function __construct() {
$this->tableName = 'user';
$this->primaryKey = 'id';
}
public function checkUser($data = array())
{
$this->db->select($this->primaryKey);
$this->db->from($this->tableName);
$con = array(
'oauth_provider' => $data['oauth_provider'],
'oauth_uid' => $data['oauth_uid']
);
$this->db->where($con);
$query = $this->db->get();
$check = $query->num_rows();
if($check > 0)
{
$result = $query->row_array();
$data['modified'] = date("Y-m-d H:i:s");
$update = $this->db->update($this->tableName, $data, array('id'=>$result['id']));
$userID = $result['id'];
}
else
{
$data['candidate_id'] = date(YmdHis);
$data['register_as'] = 'Consultant';
$data['created'] = date("Y-m-d H:i:s");
$data['modified'] = date("Y-m-d H:i:s");
$insert = $this->db->insert($this->tableName,$data);
$userID = $this->db->insert_id();
}
return $userID?$userID:false;
}
}
在此代码中,我创建了一个Google登录名,当我单击Google Login
链接时,它会在登录页面上重定向我,但当我填写登录详细信息并单击“提交”按钮时,它将再次在{{ 1}}页面,它不会在login
上重定向我,我不知道为什么?我在哪里做错了?请帮助我。
谢谢