Google APIでクライアントID、クライアントシークレットを発行し、OauthでGoogle APIにアクセスします。
<?php
require_once('config.php');
require_once('functions.php');
session_start();
if (empty($_GET['code'])){
// 認証前
// 認証ダイアログ
// csrf
$_SESSION['state'] = sha1(uniqid(mt_rand(), true));
$params = array(
'client_id' => CLIENT_ID,
'redirect_uri' => 'dev.hogehoge.com:8000/redirect.php',
'state' => $_SESSION['state'],
'approval_prompt' => 'force',
'scope' => 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email',
'response_type' => 'code'
);
// googleへ飛ばす
$url = 'https://accounts.google.com/o/oauth2/auth?'.http_build_query($params);
header('Location: '.$url);
exit;
} else {
// 認証後
// ssrf state check
if ($_SESSION['state'] != $_GET['state']){
echo "不正な処理でした!";
exit;
}
// access_tokenを取得
// ユーザー情報
// DBに格納
// ログイン処理
// index.phpへ飛ばす
}