-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_login_delete.php
207 lines (91 loc) · 4.82 KB
/
google_login_delete.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
* Copyright 2011 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
require_once 'google/src/Google_Client.php'; // include the required calss files for google login
require_once 'google/src/contrib/Google_PlusService.php';
require_once 'google/src/contrib/Google_Oauth2Service.php';
require 'google_functions.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Asig 18 Sign in with GPlus"); // Set your applicatio name
$client->setScopes(array('https://www.googleapis.com/auth/userinfo.email', 'https://www.googleapis.com/auth/plus.me')); // set scope during user login
$client->setClientId('421386846591-k2lt1retpi2sia06p6bk4otu0m51qa34.apps.googleusercontent.com'); // paste the client id which you get from google API Console
$client->setClientSecret('Wgc00hD2PMEEk-f4BnB-ToSB'); // set the client secret
$client->setRedirectUri('http://localhost/jobready/select_actor.php'); // paste the redirect URI where you given in APi Console. You will get the Access Token here during login success
$client->setDeveloperKey('Wgc00hD2PMEEk-f4BnB-ToSB'); // Developer key
$plus = new Google_PlusService($client);
$oauth2 = new Google_Oauth2Service($client); // Call the OAuth2 class for get email address
if(isset($_GET['code'])) {
$client->authenticate(); // Authenticate
$_SESSION['access_token'] = $client->getAccessToken(); // get the access token here
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if(isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$user = $oauth2->userinfo->get();
$me = $plus->people->get('me');
$optParams = array('maxResults' => 100);
$activities = $plus->activities->listActivities('me', 'public',$optParams);
// The access token may have been updated lazily.
$_SESSION['access_token'] = $client->getAccessToken();
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL); // get the USER EMAIL ADDRESS using OAuth2
} else {
$authUrl = $client->createAuthUrl();
}
if(isset($me)){
$_SESSION['gplusuer'] = $me; // start the session
//check_google_user($guid,$gemail);
}
if(isset($_GET['logout'])) {
unset($_SESSION['access_token']);
unset($_SESSION['gplusuer']);
session_destroy();
header('Location: http://localhost/jobready/index.php' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
$user = $service->userinfo->get(); //get user info
// connect to database
$mysqli = new mysqli($host_name, $db_username, $db_password, $db_name);
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
//check if user exist in database using COUNT
$result = $mysqli->query("SELECT COUNT(google_id) as usercount FROM job_registration WHERE google_id=$user->id");
$user_count = $result->fetch_object()->usercount; //will return 0 if user doesn't exist
//show user picture
//echo '<img src="'.$user->picture.'" style="float: right;margin-top: 33px;" />';
//echo 'Hi '.$user->name.', Thanks for Registering! [<a href="'.$redirect_uri.'?logout=1">Log Out</a>]';
$statement = $mysqli->prepare("INSERT INTO job_registration (google_id, email) VALUES (?,?)");
//$statement->bind_param('issss', $user->id, $user->name, $user->email, $user->link, $user->picture);
//$statement->execute();
echo $mysqli->error;
//print user details
//echo '<pre>';
//print_r($user);
//echo '</pre>';
// it will simply destroy the current seesion which you started before
#header('Location: https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
/*NOTE: for logout and clear all the session direct goole jus un comment the above line an comment the first header function */
}
?>
<?php
if(isset($authUrl)) {
echo "<a class='login' href='$authUrl'><img src=\"google-login-button-asif18.png\" alt=\"Google login using php api for your website\" title=\"login with google\" /></a>";
} else {
echo "<a class='logout' href='index.php?logout'>Logout</a>";
}
if(isset($_SESSION['gplusuer'])){ ?>
<?php } ?>