我使用google + api进行身份验证并使用CodeIgniter框架检索用户数据。
在身份验证之后,在我在代码中指定的重定向uri的末尾添加了一个随机字符串。
这导致空白视图。我该如何解决这个问题?
PS:相同的代码在localhost上运行得非常好!
controller - user_authentication.php
<?php
// Start session
session_start();
class User_Authentication extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
// Include two files from google-php-client library in controller
include_once APPPATH . "libraries/google-api-php-client-master/vendor/autoload.php";
include_once APPPATH . "libraries/google-api-php-client-master/src/Google/Client.php";
include_once APPPATH . "libraries/google-api-php-client-master/vendor/google/auth/src/OAuth2.php";
// Store values in variables from project created in Google Developer Console
$client_id = 'CLIENT ID';
$client_secret = 'SECRET';
$redirect_uri = 'http://mgdevx.byethost14.com/blackjack/index.php/user_authentication/index/';
$simple_api_key = 'KEY';
// Create Client Request to access Google API
$client = new Google_Client();
$client->setApplicationName("PHP Google OAuth Login Example");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setDeveloperKey($simple_api_key);
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
// Send Client Request
$objOAuthService = new Google_Service_Oauth2($client);
// Add Access Token to Session
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
// Set Access Token to make Request
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
}
// Get User Data from Google and store them in $data
if ($client->getAccessToken()) {
$userData = $objOAuthService->userinfo->get();
$data['userData'] = $userData;
$_SESSION['access_token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
$data['authUrl'] = $authUrl;
}
// Load view and send values stored in $data
$this->load->view('google_authentication', $data);
}
// Unset session and logout
public function logout() {
unset($_SESSION['access_token']);
redirect(base_url());
}
}
?>
查看 - google_authentication.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/style.css">
<link rel="stylesheet" media="screen and (max-width: 1200px) and (min-width: 0px)" type="text/css" href="<?php echo base_url(); ?>css/styleresponsive1.css">
<link rel="stylesheet" media="screen and (max-width: 600px) and (min-width: 0px)" type="text/css" href="<?php echo base_url(); ?>css/styleresponsive2.css">
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro|Open+Sans+Condensed:300|Raleway' rel='stylesheet' type='text/css'>
</head>
<body>
<div id="main">
<div id="envelope">
<?php if (isset($authUrl)){ ?>
<header id="sign_in">
<h2>CodeIgniter Login With Google Oauth PHP</h2>
</header>
<hr>
<div id="content">
<center><a href="<?php echo $authUrl; ?>"><img id="google_signin" src="<?php echo base_url(); ?>images/google_login.jpg" width="100%" ></a></center>
</div>
<?php }else{ ?>
<!-- <header id="info">
<a target="_blank" class="user_name" href="<?php echo $userData->link; ?>" /><img class="user_img" src="<?php echo $userData->picture; ?>" width="15%" />
<?php echo '<p class="welcome"><i>Welcome ! </i>' . $userData->name . "</p>"; ?></a><a class='logout' href='https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=<?php echo base_url(); ?>index.php/user_authentication/logout'>Logout</a>
</header> -->
<h3>Fill in some extra details</h3>
<?php
$this->session->set_userdata(array('email' => $userData->email));
$this->session->set_userdata(array('gender' => $userData->gender));
redirect('main/gregister');
?>
<?php }?>
</div>
</div>
</body>
</html>
controller - main.php
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Main extends CI_Controller {
public function index()
{
$this->load->view('main-page');
}
public function gregister()
{
$gmail = $this->session->userdata('email');
$this->load->model('model_register');
$value = $this->model_register->checkmail($gmail);
if($value == 1)
{
$this->load->view('gregister');
}
else
{
$this->load->view('success-page');
}
}
}
对于一个帐户,重定向uri为:http://mgdevx.byethost14.com/blackjack/index.php/user_authentication/index/ ?code = 4 / mjmogtOZrJxwHvR40vXdCEZ8tBgV-KWSUe8Nav6j-WA#
粗体部分会额外添加,导致空白视图。 编辑:
error_log.txt
Wed, 30 Mar 2016 01:45:42 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/favicon.ico<br>
Wed, 30 Mar 2016 02:45:27 -0500 AH01797: client denied by server configuration: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/mgdevx.byethost14.com<br>
Wed, 30 Mar 2016 02:45:27 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 02:45:27 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 02:45:27 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 02:45:27 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 02:45:39 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 02:45:39 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 02:45:39 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 02:45:39 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 02:45:49 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 02:45:49 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 02:45:49 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 02:45:49 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 02:46:01 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 02:46:01 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 02:46:01 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 02:46:01 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 05:45:32 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 05:45:32 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 05:45:32 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 05:45:32 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 07:45:42 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 07:45:42 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 07:45:42 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 07:45:42 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 07:45:52 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 07:45:52 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 07:45:52 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 07:45:52 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 08:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/favicon.ico<br>
Wed, 30 Mar 2016 08:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 08:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 08:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 08:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 09:45:26 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 09:45:26 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 09:45:26 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 09:45:26 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 09:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/favicon.ico<br>
Wed, 30 Mar 2016 09:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 09:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 09:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 09:45:37 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 09:45:48 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 09:45:48 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 09:45:48 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 09:45:48 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 09:45:48 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/favicon.ico<br>
Wed, 30 Mar 2016 09:45:59 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 09:45:59 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 09:45:59 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 09:45:59 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 10:45:52 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/favicon.ico<br>
Wed, 30 Mar 2016 12:46:04 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/favicon.ico<br>
Wed, 30 Mar 2016 14:46:03 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 14:46:03 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 14:46:03 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 14:46:03 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
Wed, 30 Mar 2016 14:46:03 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/favicon.ico<br>
Wed, 30 Mar 2016 14:46:14 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/style.css<br>
Wed, 30 Mar 2016 14:46:14 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive2.css<br>
Wed, 30 Mar 2016 14:46:14 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/css/styleresponsive1.css<br>
Wed, 30 Mar 2016 14:46:14 -0500 AH00128: File does not exist: /home/vol8_4/byethost14.com/b14_17744405/htdocs/blackjack/images/google_login.jpg<br>
答案 0 :(得分:0)
您正在使用“普通”PHP网址,但CI有自己的网址格式siteURL.com/controller/function/param1/param2
(默认情况下,您可以在config.php
$config['enable_query_strings'] = TRUE;
中更改此设置)。请访问:https://ellislab.com/codeigniter/user-guide/general/urls.html
(没有?code=
)部分重定向到所需的页面(我猜,几乎不是空白)。
然后你应该抓住参数并分配给$ code var。希望它有所帮助!
更新
你应该改变:
public function index() {
到:
public function index($code == NULL) { //$code will 'catch' the param
然后在:`
// Add Access Token to Session
if (isset($_GET['code'])) {`
更改为:
// Add Access Token to Session
if ($code != NULL) {
$client->authenticate($code);
将调用控制器user_authentication
函数index()和$ code = 4,如果你想得到mjmog....-WA#
部分,你必须在index()函数中添加另一个参数,如:{ {1}}
更新2:
确定。我猜我们正处于开始状态。我认为你有一个URL问题。我进行注册,重定向返回“500服务器错误”,通常是URL错误配置。
¿你在config.php中设置了enable_query_strings吗?: `$ config ['enable_query_strings'] = TRUE;
你有.htaccess文件吗?你能发贴吗?