2017-05-25 317 views
8

我希望能够重定向到另一个控制器,但当用户登录与谷歌和成功完整它被重定向到那里usercp,但由于某种原因,它得到从这里Codeigniter与谷歌oauth2添加hashtag php重定向('usercp')

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

当月底#使用笨重定向重定向()把它添加#它。

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

问题当重定向到新的页面,一旦成功登录如何被添加停止#。

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

我也使用虚拟主机与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'); 

    } 

} 

配置/ 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

我使用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'); 

    } 

    } 
} 

回答

2

当调用重定向,你应该能够降散列使​​用refresh param:

redirect('usercp', 'refresh'); 

你可以做这样的事情

$url = strstr($url, '#', true); 

修改URL但除此之外,因为它是一个客户端的东西有没有很多的选择。你也可以从JavaScript中删除它,当客户端加载页面

history.pushState('', document.title, window.location.pathname + window.location.search) 
+0

用PHP有没有办法用了使用'refresh' – user4419336

+0

@ wolfgang1983看看我的已更新回答 –

2

笨的重定向()函数使用PHP的header()函数在两个方面:

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

使用刷新参数不添加标签。 您可以在系统/帮助/ 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)); 
+0

什么是$ code变量? – user4419336

+0

check in system/helpers/url_helper函数重定向:302或303或307 – Vickel

+0

仍然从google url获取hashtag并将其添加到重定向 – user4419336

1

,因为这是过长的注释部分,这里有云:

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

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

做oauth,然后尝试找到重定向到您的网页的谷歌的请求。

点击请求,你会得到请求的细节。

请参阅响应标题,它应该是302状态,目标应该是您的http://www.example.com/usercp url。

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

,如果它存在的目的,那么问题出在谷歌的一部分,并没有什么可以做这件事