TShopping

標題: 如何在網頁加入google登入 auth [打印本頁]

作者: pipe    時間: 2026-3-13 17:48
標題: 如何在網頁加入google登入 auth
本帖最後由 pipe 於 2026-3-13 19:46 編輯

1. 申請 google 授權碼 (Auth Code)
說明文件

建立用戶端 ID 和用戶端密碼
如要建立用戶端 ID 和用戶端密鑰,請建立 Google API 控制台專案、設定 OAuth 用戶端 ID,並註冊 JavaScript 來源:




2. 以 https://kingliwei.com 網域為例
建立 login.php


  1. <?php
  2. include "global.php";
  3. session_start();        
  4. // Google API 設定
  5. $client_id = "your_id.apps.googleusercontent.com";

  6. if (isset($_POST['credential'])) {
  7.     $id_token = $_POST['credential'];
  8.    
  9.     // 改用 cURL 驗證,比 file_get_contents 更穩定
  10.     $ch = curl_init();
  11.     curl_setopt($ch, CURLOPT_URL, "https://oauth2.googleapis.com/tokeninfo?id_token=" . $id_token);
  12.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  13.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 若伺服器 SSL 憑證過舊,此行可避免驗證無效
  14.     $response = curl_exec($ch);
  15.     curl_close($ch);
  16.    
  17.     $user_data = json_decode($response, true);
  18.     if (isset($user_data['email'])) {
  19.         $email = addslashes($user_data['email']);
  20.         
  21.         // 檢查資料庫 (請確保 net_member 資料表有 email 欄位)
  22.         $sql = "SELECT * FROM net_member WHERE email = '$email'";
  23.         $query = $db->query($sql);
  24.         $row = $db->fetch_array($query);

  25.         if ($row) {
  26.             // 登入成功:先設定 Session,最後再執行跳轉
  27.             $_SESSION['username'] = $row['username'];
  28.             $_SESSION['uid'] = $row['uid'];
  29.             
  30.             echo "<script>alert('Google 登入成功!');window.location='index.php';</script>";
  31.             exit;
  32.         } else {
  33.             // 沒帳號,帶到註冊頁
  34.             echo "<script>alert('尚未註冊,請先完成帳號綁定。');window.location='reg.php?email=$email';</script>";
  35.             exit;
  36.         }
  37.     } else {
  38.         echo "<script>alert('Google 驗證無效');history.go(-1);</script>";
  39.         exit;
  40.     }
  41. }

  42. $smarty->display('login.htm');
  43. mysqli_close();
  44. ?>
複製代碼


3. 建立 login.htm

  1. <div id="g_id_onload"
  2.                                          data-client_id="your_id.apps.googleusercontent.com"
  3.                                          data-callback="handleCredentialResponse"
  4.                                          data-ux_mode="popup"
  5.                                          data-auto_prompt="false">
  6.                                 </div>
  7.                                 <div class="g_id_signin"
  8.                                          data-type="standard"
  9.                                          data-shape="rectangular"
  10.                                          data-theme="outline"
  11.                                          data-text="signin_with"
  12.                                          data-size="large"
  13.                                          data-logo_alignment="left">
  14.                                 </div>

  15. <script src="https://accounts.google.com/gsi/client" async defer></script>

  16. <script>
  17.   function handleCredentialResponse(response) {
  18.     // 建立一個表單自動提交
  19.     var form = document.createElement('form');
  20.     form.setAttribute('method', 'post');
  21.     form.setAttribute('action', 'login.php');

  22.     var hiddenField = document.createElement('input');
  23.     hiddenField.setAttribute('type', 'hidden');
  24.     hiddenField.setAttribute('name', 'credential'); // 這裡要對應後端的 $_POST['credential']
  25.     hiddenField.setAttribute('value', response.credential);

  26.     form.appendChild(hiddenField);
  27.     document.body.appendChild(form);
  28.     form.submit();
  29.   }
  30. </script>
複製代碼


展示圖
(, 下載次數: 10)





歡迎光臨 TShopping (https://www.tshopping.com.tw/) Powered by Discuz! X3.5