2012-04-23 5 views
0

このコードをCodeIgniterに使用しようとしています。 'ソース'のタブをクリックすると、元のソースコードを見ることができます。元のソースを使用するとうまく動作します。私はそれを使用する場合

http://dotinstall.com/lessons/google_connect_php/5017

コントローラ

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Welcome extends CI_Controller { 


    function __construct() 
    { 
     parent::__construct(); 
     $this->config->load('google_connect');// this one has client_id and client_secret 
     $this->load->helper('functions'); 
     $this->load->helper('url'); 
     $this->load->model('mgoogle_connect'); 
     session_start(); 
    } 


    public function index() 
    { 
     //$data['client_id'] =$this->config->item('CLIENT_ID'); 
     if (empty($_SESSION['user'])) 
     { 
      redirect('welcome/login','refresh'); 
     } 
     $this->load->view('index'); 
    } 


    function redirect() 
    { 
     $client_id=$this->config->item('client_id'); 
     $client_secret = $this->config->item('client_secret'); 
     if (empty($_GET['code'])) 
     { 
      // 認証前の処理 

      // CSRF対策 
      $_SESSION['state'] = sha1(uniqid(mt_rand(), true)); 

      // 認証ダイアログの作成 
      $params = array(
       'client_id' => $client_id, 
       'redirect_uri' => site_url('welcome/redirect'), 
       'state' => $_SESSION['state'], 
       'approval_prompt' => 'force', 
       'scope' => 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email', 
       'response_type' => 'code' 
      ); 
      $url = "https://accounts.google.com/o/oauth2/auth?".http_build_query($params); 

      // go to google 
      redirect($url); 
      //header('Location: '.$url); 
      //exit; 
     } 
     else 
     { 
      // 認証後の処理 

      // CSRF対策 
      if ($_SESSION['state'] != $_GET['state']) { 
       echo "何がおかしい!"; 
       exit; 
      } 

      // get profile info 

      $params = array(
       'client_id' => $client_id, 
       'client_secret' => $client_secret, 
       'code' => $_GET['code'], 
       'redirect_uri' => site_url('welcome/redirect'), 
       'grant_type' => 'authorization_code' 
      ); 
      $url = 'https://accounts.google.com/o/oauth2/token'; 

      $curl = curl_init(); 
      curl_setopt($curl, CURLOPT_URL, $url); 
      curl_setopt($curl, CURLOPT_POST, 1); 
      curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params)); 
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 

      $rs = curl_exec($curl); 
      curl_close($curl); 

      $json = json_decode($rs); 

      $url = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token='.$json->access_token; 
      $me = json_decode(file_get_contents($url)); 

      // enter to DB 

      //$user = $this->mgoogle_connect->insertuser($me); 

      $data['user']=$me; 

      // login 
      if (!empty($user)) { 
       session_regenerate_id(true); 
       $_SESSION['user'] = $user; 
      } 

      // send it to login 
      redirect('/'); 
     } 
    } 


    function login() 
    { 
     $data['client_id']=$this->config->item('client_id'); 
     $data['client_secret'] = $this->config->item('client_secret'); 
     $this->load->view('login',$data); 
    } 

    function logout() 
    { 

     $_SESSION = array(); 

     if (isset($_COOKIE[session_name()])) { 
      setcookie(session_name(), '', time() - 86400, '/ci210/'); 
     } 

     session_destroy(); 

     redirect('/'); 
    } 
} 

/* End of file welcome.php */ 
/* Location: ./application/controllers/welcome.php */ 

ヘルパー..私は私がログインページにリダイレクトしていますので、$_GET['code']によって「コード」を取得することはできませんよそうです/functions_helper.php

<?php 

function h($s) { 
    return htmlspecialchars($s); 
} 

function r($s) { 
    return mysql_real_escape_string($s); 
} 

function jump($s) { 
    redirect (site_url($s)); 
    //header('Location: '.site_url().$s); 
    //exit; 
} 

view/login.p HP

<!DOCTYPE html> 
<html lang="ja"> 
<head> 
    <meta charset="UTF-8"> 
    <title>login</title> 
</head> 
<body> 
<p><a href="redirect">Login with Google account</a></p> 

</body> 
</html> 

ビュー/ index.phpを

<!DOCTYPE html> 
<html lang="ja"> 
<head> 
    <meta charset="UTF-8"> 
    <title>HOME</title> 
</head> 
<body> 
<p><?php echo h($_SESSION['user']['google_name']); ?>(<?php echo h($_SESSION['user']['google_email']); ?>)You are logged in.</p> 
<p><a href="logout">[logout]</a></p 

</body> 
</html> 

答えて

1

application/config/config.phpにクエリ文字列を有効にしてください。デフォルトでは、CodeIgniterはこれを無効にします。

$config['enable_query_strings'] = TRUE; 
関連する問題