AKSHAT007HERE


SUBMITTED BY: AKSHAT007HERE

DATE: Feb. 22, 2017, 9:26 a.m.

FORMAT: PHP

SIZE: 2.7 kB

HITS: 729

  1. <?php
  2. $client = new oauth_client_class;
  3. // set the offline access only if you need to call an API
  4. // when the user is not present and the token may expire
  5. $client->offline = FALSE;
  6. $client->debug = false;
  7. $client->debug_http = true;
  8. $client->redirect_uri = REDIRECT_URL;
  9. $client->client_id = CLIENT_ID;
  10. $application_line = __LINE__;
  11. $client->client_secret = CLIENT_SECRET;
  12. if (strlen($client->client_id) == 0 || strlen($client->client_secret) == 0)
  13. die('Please go to Google APIs console page ' .
  14. 'http://code.google.com/apis/console in the API access tab, ' .
  15. 'create a new client ID, and in the line ' . $application_line .
  16. ' set the client_id to Client ID and client_secret with Client Secret. ' .
  17. 'The callback URL must be ' . $client->redirect_uri . ' but make sure ' .
  18. 'the domain is valid and can be resolved by a public DNS.');
  19. /* API permissions
  20. */
  21. $client->scope = SCOPE;
  22. if (($success = $client->Initialize())) {
  23. if (($success = $client->Process())) {
  24. if (strlen($client->authorization_error)) {
  25. $client->error = $client->authorization_error;
  26. $success = false;
  27. } elseif (strlen($client->access_token)) {
  28. $success = $client->CallAPI(
  29. 'https://www.googleapis.com/oauth2/v1/userinfo', 'GET', array(), array('FailOnAccessError' => true), $user);
  30. }
  31. }
  32. $success = $client->Finalize($success);
  33. }
  34. if ($client->exit)
  35. exit;
  36. if ($success) {
  37. // Now check if user exist with same email ID
  38. $sql = "SELECT COUNT(*) AS count from google_users where email = :email_id";
  39. try {
  40. $stmt = $DB->prepare($sql);
  41. $stmt->bindValue(":email_id", $user->email);
  42. $stmt->execute();
  43. $result = $stmt->fetchAll();
  44. if ($result[0]["count"] > 0) {
  45. // User Exist
  46. $_SESSION["name"] = $user->name;
  47. $_SESSION["email"] = $user->email;
  48. $_SESSION["new_user"] = "no";
  49. } else {
  50. // New user, Insert in database
  51. $sql = "INSERT INTO `google_users` (`name`, `email`) VALUES " . "( :name, :email)";
  52. $stmt = $DB->prepare($sql);
  53. $stmt->bindValue(":name", $user->name);
  54. $stmt->bindValue(":email", $user->email);
  55. $stmt->execute();
  56. $result = $stmt->rowCount();
  57. if ($result > 0) {
  58. $_SESSION["name"] = $user->name;
  59. $_SESSION["email"] = $user->email;
  60. $_SESSION["new_user"] = "yes";
  61. $_SESSION["e_msg"] = "";
  62. }
  63. }
  64. } catch (Exception $ex) {
  65. $_SESSION["e_msg"] = $ex->getMessage();
  66. }
  67. $_SESSION["user_id"] = $user->id;
  68. } else {
  69. $_SESSION["e_msg"] = $client->error;
  70. }
  71. header("location:home.php");
  72. exit;
  73. ?>

comments powered by Disqus