Codeigniter with google oauth2 adds hashtag php for redirection ('usercp')

I want to be able to redirect to another controller, but when a user logs in with google and successfully works, he is redirected to usercp there, but for some reason he gets # from the end here.

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

And when redirecting using codeigniter redirect (), it adds # to it.

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

Question When redirecting to a new page after a successful login, how to stop # from adding.

I am using https://github.com/moemoe89/google-login-ci3

I also use vhost with xammp

Controller function

 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'); } } 
Settings

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

I am using HMVC with codeigniter

applications / modules / accounts / controllers / login.php

Full controller

 <?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'); } } } 
+8
codeigniter google-api google-login google-plus-signin
source share
3 answers

When calling a redirect, you can remove the hash using the refresh parameter:

 redirect('usercp', 'refresh'); 

You can change the URL by doing something like

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

But otherwise, as there are not many options on the client side. You can also remove it from javascript when the client loads the page using

 history.pushState('', document.title, window.location.pathname + window.location.search) 
+2
source share

The codeigniter redirect () function uses the php header () function in two ways:

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

using the refresh parameter will not add a hashtag. You will find out about this in the system / helpers / url _helper.php

you can take advantage of this when modifying google_login.php

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

respectively

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

as this is too long in the comments section, it says here:

try using browser debugging mode / developer tools and see its network part. there, you can see the sequence of requests when your page loads.

if you use chrome, the thick preserve log parameter before doing oauth.

run the oauth command and then try to find a google request that is redirected to your page.

click on the request, you will receive information about the request.

see the response header, it should be 302, and the destination address should be your URL http://www.example.com/usercp .

if you do not see #, then you have problems on your part, try checking your .htaccess file.

if it is at the destination, then the problem is with the google part and you cannot do a bit with it.

+1
source share

All Articles