[英]How to login with Google Account from my PHP application?
我需要你的幫助。 我已經創建了一個PHP應用程序,並且它具有登錄身份驗證才能訪問它。 我的用戶表是這樣的:
===========================================================
username | password | email | e_pass
===========================================================
admin | admin | mymail@google.com | myGooglePassword
===========================================================
用戶名和密碼列用於本地身份驗證,然后電子郵件和e_pass用於Google身份驗證。 那么,我該怎么做呢? 本地身份驗證和Google身份驗證是在同一步驟完成的嗎? 因為Google日歷和Google雲端硬盤已嵌入我的PHP應用程序中,並且需要Google身份驗證才能訪問它們。
這是我的身份驗證代碼和PHP代碼:
<?php
if (empty($_POST['userid']) or empty($_POST['passwd'])) { // jika file diakses secara langsung ?>
<script type="text/javascript">
alert("You can't access this file directly!");
</script>";
<script> document.location.href='index.php'; </script>
<?php
}
else {
include "conn.php";
$user = $_POST['userid'];
$pass = sha1($_POST['passwd']);
$sql = mysql_query("select * from access where username = '$user'") or die (mysql_error());
$num = mysql_num_rows($sql);
if ($num <> 0) { // jika username ditemukan dalam tabel access
$sql = mysql_query("select * from access where password = '$pass'") or die (mysql_error());
$num = mysql_num_rows($sql);
if ($num <> 0) { // jika password ditemukan dalam table access
$sql = mysql_query("select * from access where username = '$user' and password = '$pass'") or die (mysql_error());
$data = mysql_fetch_array($sql);
// Google Authentication Code supposed to be here
if ($data['unit'] == 'operational') { // jika unit operational
session_start();
$_SESSION['id'] = $data['id'];
$_SESSION['unit'] = $data['unit'];
$_SESSION['sub'] = $data['subunit'];
if ($data['subunit'] == 'manager') { // jika berstatus manager ?>
<script>document.location.href='operational/manager/operational_manager.php?page=home';</script>
<?php
}
else if ($data['subunit'] == 'facility') { ?>
<script>document.location.href='operational/facility/facility.php?page=home';</script>
<?php
}
else if ($data['subunit'] == 'transport') { ?>
<script>document.location.href='operational/transportation/transport.php?page=home';</script>
<?php
}
else if ($data['subunit'] == 'procure') { ?>
<script>document.location.href='operational/procurement/procure.php?page=home';</script>
<?php
}
else if ($data['subunit'] == 'it') { ?>
<script>document.location.href='operational/it/it.php?page=home';</script>
<?php
}
else if ($data['subunit'] == 'accounting') { ?>
<script>document.location.href='operational/accounting/account.php?page=home';</script>
<?php
}
}
else if ($data['unit'] == 'academic') { // jika unit academic
session_start();
$_SESSION['id'] = $data['id'];
$_SESSION['unit'] = $data['unit'];
$_SESSION['sub'] = $data['subunit'];
if ($data['subunit'] == 'manager') { // jika berstatus manager ?>
<script>document.location.href='academic/manager/academic_manager.php?page=home';</script>
<?php
}
else { ?>
<script>document.location.href='academic/academic.php?page=home';</script>
<?php
}
}
else if ($data['unit'] == 'bisdev') { // jika unit bisdev
session_start();
$_SESSION['id'] = $data['id'];
$_SESSION['unit'] = $data['unit'];
$_SESSION['sub'] = $data['subunit'];
if ($data['subunit'] == 'manager') { // jika berstatus manager ?>
<script>document.location.href='bisdev/manager/bisdev_manager.php?page=home';</script>
<?php
}
else { ?>
<script>document.location.href='bisdev/bisdev.php?page=home';</script>
<?php
}
}
else if ($data['unit'] == 'admin') { // jika admin
session_start();
$_SESSION['id'] = $data['id'];
$_SESSION['unit'] = $data['unit'];
$_SESSION['sub'] = $data['subunit'];
?>
<script>document.location.href='admin/admin.php?page=home';</script>
<?php
}
else if ($data['unit'] == 'director') { // jika director
session_start();
$_SESSION['id'] = $data['id'];
$_SESSION['unit'] = $data['unit'];
$_SESSION['sub'] = $data['subunit'];
?>
<script>document.location.href='director/director.php?page=home';</script>
<?php
}
}
else if ($num == 0){ // jika password tidak ditemukan dalam table access
?>
<script type="text/javascript">
alert("Incorrect password!");
</script>";
<script> document.location.href='index.php'; </script>
<?php
}
}
else if($num == 0) { // jika username tidak ditemukan dalam tabel access
?>
<script type="text/javascript">
alert("Username is not registered!");
</script>";
<script> document.location.href='index.php'; </script>
<?php
}
}
?>
您不應直接使用電子郵件和密碼。 您應該使用OAuth 2.0。 您可以在此處了解有關OAuth的更多信息。
您應該存儲用戶的訪問令牌,而不是電子郵件ID的密碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.