Google使用Codeigniter登录后如何重定向? [英] How to redirect after google login using codeigniter?

查看:104
本文介绍了Google使用Codeigniter登录后如何重定向?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器:

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

view: login.php

<a href="<?php echo $loginURL; ?>">Google Login</a>

型号:Google_user.php

model: 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链接时,它会正常工作,它会在登录页面上重定向我,但是当我填写登录详细信息并单击提交"按钮时,它会再次在login页面上重定向我而且它不重定向我在profile page上我不知道为什么?我在哪里做错了?请帮助我.

In this code, I have created a google login which is working when I click on Google Login link it redirects me on login page but when I fill login detail and click on submit button it again redirect me on login page and it doesn't redirect me on profile page I don't know why? where am I doing wrong? Please help me.

谢谢

推荐答案

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

Maybe late respond, as you can see when setting up the console, you need to add URL redirect when authenticating, please see image below. Thanks

这篇关于Google使用Codeigniter登录后如何重定向?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆