Advertisement
Guest User

Untitled

a guest
Feb 11th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. <?php
  2. session_start(); //session start
  3.  
  4. require_once ('libraries/Google/autoload.php');
  5.  
  6. //Insert your cient ID and secret
  7. //You can get it from : https://console.developers.google.com/
  8. $client_id = '---client_id---.apps.googleusercontent.com';
  9. $client_secret = '---client_secret---';
  10. $redirect_uri = 'http://localhost/test/glogin/glogin.php';
  11.  
  12. //database
  13. $db_username = "root"; //Database Username
  14. $db_password = ""; //Database Password
  15. $host_name = "localhost"; //Mysql Hostname
  16. $db_name = 'test'; //Database Name
  17.  
  18. //incase of logout request, just unset the session var
  19. if (isset($_GET['logout'])) {
  20.   unset($_SESSION['access_token']);
  21.    
  22. }
  23.  
  24. /************************************************
  25.   Make an API request on behalf of a user. In
  26.   this case we need to have a valid OAuth 2.0
  27.   token for the user, so we need to send them
  28.   through a login flow. To do this we need some
  29.   information from our API console project.
  30.  ************************************************/
  31. $client = new Google_Client();
  32. $client->setClientId($client_id);
  33. $client->setClientSecret($client_secret);
  34. $client->setRedirectUri($redirect_uri);
  35. $client->addScope("email");
  36. $client->addScope("profile");
  37.  
  38. /************************************************
  39.   When we create the service here, we pass the
  40.   client to it. The client then queries the service
  41.   for the required scopes, and uses that when
  42.   generating the authentication URL later.
  43.  ************************************************/
  44. $service = new Google_Service_Oauth2($client);
  45.  
  46. /************************************************
  47.   If we have a code back from the OAuth 2.0 flow,
  48.   we need to exchange that with the authenticate()
  49.   function. We store the resultant access token
  50.   bundle in the session, and redirect to ourself.
  51. */
  52.    
  53. if (isset($_GET['code'])) {
  54.   $client->authenticate($_GET['code']);
  55.   $_SESSION['access_token'] = $client->getAccessToken();
  56.   header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  57.   exit;
  58. }
  59.  
  60. /************************************************
  61.   If we have an access token, we can make
  62.   requests, else we generate an authentication URL.
  63.  ************************************************/
  64. if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
  65.   $client->setAccessToken($_SESSION['access_token']);
  66. } else {
  67.   $authUrl = $client->createAuthUrl();
  68.   
  69. }
  70.  
  71.  
  72. //Display user info or display login url as per the info we have.
  73. echo '<div style="margin:20px">';
  74.  if (isset($authUrl)){
  75.     // show login url
  76.     echo '<div align="center">';
  77.  echo '<h3>Login with Google -- Demo</h3>';
  78.     echo '<div>Please click login button to connect to Google.</div>';
  79.     echo '<a class="login" href="' . $authUrl . '"><img src="images/google-login-button.png" /></a>';
  80.     echo '</div>';
  81.      
  82.      
  83.   }
  84.  else{
  85.      
  86.     $user = $service->userinfo->get(); //get user info
  87.      
  88.     // connect to database
  89.     $mysqli = new mysqli($host_name, $db_username, $db_password, $db_name);
  90.     if ($mysqli->connect_error) {
  91.         die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
  92.     }
  93.      
  94.     //check if user exist in database using COUNT
  95.     $result = $mysqli->query("SELECT COUNT(google_id) as usercount FROM google_users WHERE google_id=$user->id");
  96.     $user_count = $result->fetch_object()->usercount; //will return 0 if user doesn't exist
  97.      
  98.     //show user picture
  99.     echo '<img src="'.$user->picture.'" style="float: right;margin-top: 33px;" />';
  100.      
  101.     if($user_count) //if user already exist change greeting text to "Welcome Back"
  102.     {
  103.         echo 'Welcome back '.$user->name.'! [<a href="'.$redirect_uri.'?logout=1">Log Out</a>]';
  104.         $_SESSION['login_user']= $user->id;
  105.         $_SESSION['login_name']= $user->name;
  106.         $_SESSION['picture']= $user->picture;
  107.         $_SESSION['login_email']= $user->email;
  108.         header('Location: ../index.php');
  109.     }
  110.     else //else greeting text "Thanks for registering"
  111.     {
  112.         echo 'Hi '.$user->name.', Thanks for Registering! [<a href="'.$redirect_uri.'?logout=1">Log Out</a>]';
  113.         $statement = $mysqli->prepare("INSERT INTO google_users (google_id, google_name, google_email, google_link, google_picture_link) VALUES (?,?,?,?,?)");
  114.         $statement->bind_param('issss', $user->id,  $user->name, $user->email, $user->link, $user->picture);
  115.         $statement->execute();
  116.         $_SESSION['login_user']= $user->id;
  117.         $_SESSION['picture']= $user->picture;
  118.         $_SESSION['login_name']= $user->name;
  119.         $_SESSION['login_email']= $user->email;
  120.         header('Location: ../index.php');
  121.         echo $mysqli->error;
  122.     }
  123.      
  124.     //print user details
  125.     echo '<pre>';
  126.     print_r($user);
  127.     echo '</pre>';
  128. }
  129. echo '</div>';
  130.  
  131.  
  132. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement