Advertisement
Guest User

Untitled

a guest
Nov 26th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.65 KB | None | 0 0
  1. <?php
  2.  
  3. require_once ('libraries/Google/autoload.php');
  4.  
  5. session_start(); //session start
  6.  
  7.  
  8. $client_id = 'xxxxxxxxxxxxxxx.apps.googleusercontent.com';
  9. $client_secret = 'xxxxxxxxxxxxxxxxxxxx';
  10. $redirect_uri = 'http://localhost/google2/auth.php';
  11.  
  12. //database
  13. $db_username = "root"; //Database Username
  14. $db_password = ""; //Database Password
  15. $host_name = "xxxxxxxxx"; //Mysql Hostname
  16. $db_name = 'xxxxxxxx'; //Database Name
  17.  
  18.  
  19. $client = new Google_Client();
  20. $client->setClientId($client_id);
  21. $client->setClientSecret($client_secret);
  22. $client->setRedirectUri($redirect_uri);
  23. $client->addScope("email");
  24. $client->addScope("profile");
  25.  
  26.  
  27. if (!isset ($_GET['code'])) {
  28. $auth_url = $client->createAuthUrl();
  29. header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
  30. }
  31. else {
  32. $client->authenticate($_GET['code']);
  33. $_SESSION['access_token'] = $client->getAccessToken();
  34.  
  35. try {
  36. $service = new Google_Service_Oauth2($client);
  37. // $_SESSION['access_profile'] = $service->userinfo->get();
  38. $user = $service->userinfo->get(); //get user info
  39. $user = $_SESSION['access_profile'];
  40.  
  41. // connect to database
  42. $mysqli = new mysqli($host_name, $db_username, $db_password, $db_name);
  43. if ($mysqli->connect_error) {
  44. die ('Error : (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
  45.  
  46. }
  47.  
  48. //check if user exist in database using COUNT
  49.  
  50. $result = $mysqli->query("SELECT COUNT(google_id) as usercount FROM google_users WHERE google_id=$user->id");
  51. $user_count = $result->fetch_object()->usercount; //will return 0 if user doesn't exist
  52.  
  53. //masukin ke mysql nih//
  54. $statement = $mysqli->prepare("INSERT INTO google_users (google_id, google_name, google_email, google_link, google_picture_link) VALUES (?,?,?,?,?)");
  55. $statement->bind_param('issss', $user->id, $user->name, $user->email, $user->link, $user->picture);
  56. $statement->execute();
  57. echo $mysqli->error;
  58.  
  59.  
  60.  
  61. }
  62. catch (Exception $e) {
  63. echo $e->__toString();
  64. unset ($_SESSION['access_token']);
  65. die;
  66. }
  67. header('Location:afterlogin.php');
  68. }
  69.  
  70. ?>
  71.  
  72. <?php
  73.  
  74.  
  75. session_start();
  76.  
  77. if (isset ($_SESSION['access_token']) && $_SESSION['access_token']) {
  78. $user = $_SESSION['access_profile'];
  79. //print user details
  80. echo '<pre>';
  81. print_r($_SESSION['access_profile']);
  82. echo '</pre>';
  83.  
  84. }
  85.  
  86. ?>
  87.  
  88. <?php
  89.  
  90. require_once ('libraries/Google/autoload.php');
  91.  
  92. session_start(); //session start
  93.  
  94. if (isset ($_SESSION['access_token']) && $_SESSION['access_token']) {
  95. header('Location:afterlogin.php');
  96. }
  97. else {
  98. echo "<a href='auth.php'> Login dengan akun Google</a>";
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement