Codeigniter与谷歌oauth2添加hashtag php重定向(' usercp')

时间:2017-05-25 05:42:48

标签: codeigniter google-api google-login google-plus-signin

我希望能够重定向到另一个控制器,但是当用户使用google登录并且成功完成时,它会被重定向到usercp但是由于某种原因它会从这里结束#

http://www.example.com/test/google?code=4/sorrynocodeshown#

使用codeigniter redirect()进行重定向时,会向其添加#。

http://www.example.com/usercp#
  

问题成功登录后重定向到新页面时如何停止添加#。

我使用https://github.com/moemoe89/google-login-ci3

我也使用vhost和xammp

控制器功能

public function google() {

    if ($this->input->get('code')) { 

    $googleplus_auth = $this->googleplus->getAuthenticate();

    $googleplus_info = $this->googleplus->getUserInfo();

    $google_data = array(
        'google_id' => $googleplus_info['id'],
        'google_name' => $googleplus_info['name'],
        'google_link' => $googleplus_info['link'],
        'image' => $googleplus_info['picture'],
        'email' => $googleplus_info['email'],
        'firstname' => $googleplus_info['given_name'],
        'lastname' => $googleplus_info['family_name']
    );

    $login_google_userid = $this->login_model->login_with_google($googleplus_info['id'], $google_data);

    $_SESSION['user_id'] = $login_google_userid;

    redirect('usercp');

   }

}

config / googleplus.php设置

<?php

defined('BASEPATH') OR exit('No direct script access allowed');

$config['googleplus']['application_name'] 'Somename';
$config['googleplus']['client_id']        = '*****';
$config['googleplus']['client_secret']    = '*****';
$config['googleplus']['redirect_uri']     = 'http://www.mysetname.com/account/login/google';
$config['googleplus']['api_key']          = '*****';
$config['googleplus']['scopes']           = array();

enter image description here

我正在使用带有codeigniter的HMVC

应用/模块/帐户/控制器/ login.php中

完整控制器

<?php

class Login extends MX_Controller {

    private $error = array();

    public function __construct() {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->library('googleplus');
    }

    public function index() {


        if ($this->login_model->is_logged_in()) {
            $this->session->set_flashdata('success', 'Welcome back! If you wish to logout ' . anchor('account/logout', 'Click Here'));
            redirect(base_url('usercp'));
        }

        if (($this->input->server("REQUEST_METHOD") == 'POST') && $this->validateForm()) {
            $this->load->model('account/login_model');

            $user_info = $this->login_model->get_user($this->input->post('username'));

            if ($user_info) {

                $_SESSION['user_id'] = $user_info['user_id'];

                redirect(base_url('usercp'));
            }
        }

        $data['login_url'] = $this->googleplus->loginURL();

        if (isset($this->error['warning'])) {
            $data['error_warning'] = $this->error['warning'];
        } else {
            $data['error_warning'] = '';
        }

        if (isset($this->error['username'])) {
            $data['error_username'] = $this->error['username'];
        } else {
            $data['error_username'] = '';
        }

        if (isset($this->error['password'])) {
            $data['error_password'] = $this->error['password'];
        } else {
            $data['error_password'] = '';
        }

        // Common
        $data['header'] = Modules::run('common/header/index');
        $data['navbar'] = Modules::run('common/navbar/index');
        $data['footer'] = Modules::run('common/footer/index');

        $this->load->view('login', $data);
    }

    public function validateForm() {
        $this->form_validation->set_rules('username', 'username', 'required');
        $this->form_validation->set_rules('password', 'password', 'required');

        if ($this->form_validation->run() == FALSE) {

            $this->error['username'] = form_error('username', '<div class="text-danger">', '</div>');

            $this->error['password'] = form_error('password', '<div class="text-danger">', '</div>');
        }

        if ($this->input->post('username') && $this->input->post('password')) {

            $this->load->model('account/login_model');

            if (!$this->login_model->verify_password($this->input->post('username'), $this->input->post('password'))) {
                $this->error['warning'] = 'Incorrect login credentials';
            }

        }

        return !$this->error;
    }

    public function google() {

        if ($this->input->get('code')) {

        $googleplus_auth = $this->googleplus->getAuthenticate();

        $googleplus_info = $this->googleplus->getUserInfo();

        $google_data = array(
            'google_id' => $googleplus_info['id'],
            'google_name' => $googleplus_info['name'],
            'google_link' => $googleplus_info['link'],
            'image' => $googleplus_info['picture'],
            'email' => $googleplus_info['email'],
            'firstname' => $googleplus_info['given_name'],
            'lastname' => $googleplus_info['family_name']
        );

        $login_google_userid = $this->login_model->login_with_google($googleplus_info['id'], $google_data);

        $_SESSION['user_id'] = $login_google_userid;

        redirect('usercp');

    }

    }
}

3 个答案:

答案 0 :(得分:2)

调用重定向时,您应该能够使用refresh参数删除哈希:

redirect('usercp', 'refresh');

您可以通过执行类似

的操作来修改网址
$url = strstr($url, '#', true);

但是,因为它是客户端的东西,所以没有很多选择。当客户端使用

加载页面时,您也可以从javascript中删除它
history.pushState('', document.title, window.location.pathname + window.location.search)

答案 1 :(得分:2)

Codeigniter的redirect()函数以两种方式使用php header()函数:

    switch ($method)
    {
        case 'refresh':
            header('Refresh:0;url='.$uri);
            break;
        default:
            header('Location: '.$uri, TRUE, $code);
            break;
    }

使用refresh参数不会添加#标签。 您可以在system / helpers / url_helper.php

中找到更多相关信息

您可以在google_login.php更改

中使用此优势
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));

相应地

$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Refresh:0;url=' . filter_var($redirect, FILTER_SANITIZE_URL));

答案 2 :(得分:1)

因为在评论部分这个太长了,所以这里是:

尝试使用浏览器的调试模式/开发人员工具,并查看其中的网络部分。在那里,您可以在加载页面时看到请求的顺序。

如果您正在使用chrome,请在执行oauth之前加厚preserve log选项。

执行oauth,然后尝试找到重定向到您网页的Google请求。

点击请求,您将获得请求的详细信息。

查看响应标头,它应该是302状态,目标应该是您的http://www.example.com/usercp网址。

如果您没有看到#,则表示您遇到问题,请尝试检查您的.htaccess文件。

如果它在目的地,那么问题在于谷歌部分,你可以做的不多了