0

我正在尝试将此代码用于 CodeIgniter。如果您单击带有“ソース”的选项卡之一,那么您将能够看到原始源代码。当我使用原始来源时,它工作正常。

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

当我使用它时,我似乎无法获取“代码”,$_GET['code']因此我被重定向到登录页面..

控制器

<?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 */

助手/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;
}

查看/login.php

<!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']); ?>(&lt;?php echo h($_SESSION['user']['google_email']); ?>)You are logged in.</p>
<p><a href="logout">[logout]</a></p

</body>
</html>
4

1 回答 1

1

尝试在application/config/config.php. 默认情况下,CodeIgniter 禁用此功能。

$config['enable_query_strings'] = TRUE;
于 2012-04-23T05:46:28.300 回答