Google使用Codeigniter登录后如何重定向?

时间:2018-11-14 10:12:12

标签: php codeigniter google-api google-oauth google-api-php-client

控制器:

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上重定向我,我不知道为什么?我在哪里做错了?请帮助我。

谢谢

2 个答案:

答案 0 :(得分:0)

当您要在工作所在的控制器中重定向它时,只需编写以下代码即可。

$this->load->helper('url');

    if ($some_value === FALSE/TRUE)  {//You may give 0/1 as well,its up to your logic

             redirect('same_controller/method', 'refresh');
    }

并检查您是否获得$_GET['code']

答案 1 :(得分:0)

也许稍后响应,如您在设置控制台时看到的那样,您需要在身份验证时添加URL重定向,请参见下图。谢谢

enter image description here