Created
July 20, 2020 01:17
-
-
Save s3va/31daccf4a2c4c84ead19876039706084 to your computer and use it in GitHub Desktop.
Super Market With OAuth2.0 from Google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include ('config.php'); | |
require_once 'mysqlini.php'; | |
$login_button = ''; | |
if (isset($_GET["code"])) { | |
$token = $google_client->fetchAccessTokenWithAuthCode($_GET["code"]); | |
if (! isset($token['error'])) { | |
$google_client->setAccessToken($token['access_token']); | |
$_SESSION['access_token'] = $token['access_token']; | |
$_SESSION['token'] = $token; | |
$google_service = new Google_Service_Oauth2($google_client); | |
$data = $google_service->userinfo->get(); | |
if (! empty($data['given_name'])) { | |
$_SESSION['user_first_name'] = $data['given_name']; | |
} | |
if (! empty($data['family_name'])) { | |
$_SESSION['user_last_name'] = $data['family_name']; | |
} | |
if (! empty($data['email'])) { | |
$_SESSION['user_email_address'] = $data['email']; | |
} | |
if (! empty($data['gender'])) { | |
$_SESSION['user_gender'] = $data['gender']; | |
} | |
if (! empty($data['picture'])) { | |
$_SESSION['user_image'] = $data['picture']; | |
} | |
if (! empty($data['id'])) { | |
$_SESSION['google_userid'] = $data['id']; | |
} | |
} | |
header('Location: https://' . $GLOBALS['_SERVER']['SERVER_NAME'] . '/l'); | |
$mysql = mysqli_connect('localhost', $msqluname, $msqlpassw, $msqldatab); | |
if (isset($mysql)) { | |
if (! empty($data['id'])) { | |
$q = mysqli_query($mysql, "SELECT * from googleusers WHERE id = " . $data['id']); | |
if (mysqli_num_rows($q) == 0) { | |
$qstr='insert googleusers (name,givenName,familyName,id,lastlogin,picture,verifiedEmail,locale,email,gender) values ("'. | |
$data['name'].'","'. | |
$data['givenName'].'","'. | |
$data['familyName'].'","'. | |
$data['id'].'",NOW(),"'. | |
$data['picture'].'","'. | |
$data['verifiedEmail'].'","'. | |
$data['locale'].'","'. | |
$data['email'].'","'. | |
$data['gender'].'")'; | |
if (mysqli_query($mysql,$qstr)) { | |
error_log("insert success"); | |
} else { | |
error_log($qstr); | |
error_log("intert error------------------------------------------------\n"); | |
error_log(mysqli_error($mysql)); | |
} | |
}else{ | |
$qstr='update googleusers set lastlogin=NOW() where id = '.$data['id']; | |
if (mysqli_query($mysql, $qstr)) { | |
error_log("update lastlogin success"); | |
} else { | |
error_log(mysqli_error($mysql)); | |
error_log($qstr); | |
} | |
} | |
} | |
} | |
} | |
if (! isset($_SESSION['access_token'])) { | |
$login_button = '<a href="' . $google_client->createAuthUrl() . '"><img src="https://developers.google.com/identity/images/btn_google_signin_dark_normal_web.png" /></a>'; | |
} | |
?> | |
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<title>PHP Login using Google Account</title> | |
<script | |
src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> | |
</head> | |
<body> | |
<h2 align="center">PHP Login using Google Account</h2> | |
<?php | |
if ($login_button == '') { | |
echo '<img src="' . $_SESSION["user_image"] . '" />'; | |
echo '<h3><b>Name :</b> ' . $_SESSION['user_first_name'] . ' ' . $_SESSION['user_last_name'] . '</h3>'; | |
echo '<h3><b>Email :</b> ' . $_SESSION['user_email_address'] . '</h3>'; | |
echo '<h3><a href="logout.php">Logout</a></h3>'; | |
echo ' <div style="resize:both;overflow: auto;height:100%; width:650"> <iframe src="https://market.kvakva.tk" height="100%" width="100%" title="Super Market of Seva" ></iframe></div>'; | |
} else { | |
echo '<div align="center">' . $login_button . '</div>'; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment