• How To Login with Google using PHP API library

    Implementing Google login system in your website is very easy with OAuth2 support, a simple and standard authorization method. With minimal line of PHP codes, we can acquire required user information from Google, and use the information to register or login users on click of a button.

    Download link : https://code.google.com/p/google-api-php-client/downloads/list
    Database:

    CREATE TABLE IF NOT EXISTS `google_users` (
    `google_id` decimal(21,0) NOT NULL,
    `google_name` varchar(60) NOT NULL,
    `google_email` varchar(60) NOT NULL,
    `google_link` varchar(60) NOT NULL,
    `google_picture_link` varchar(60) NOT NULL,
    PRIMARY KEY (`google_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    									

    Login and Process Page

    You need to acquire client id, secret key and developer key from Google API console, If you are not sure how, please have a look at Creating Google API keys.

    Replace Google settings with Client keys and Developer key you’ve obtained from Google, and MySql database details of your website.

    You need to read comment lines in the PHP code to understand how Google login system works.

    When user clicks login link, user is redirected to Google Authentication page, once user grants the basic permission to access their data, user is again redirected back to website with Authentication code. The code is used to obtain Access Token, using Access Token the application can access current user data from Google, which could be used to register and login the user.

    <?php
    ########## Google Settings. #############
    $google_client_id   = 'xxxxxxxxxx.apps.googleusercontent.com';
    $google_client_secret   = 'xxxxxxxxxxxxxxxxxxxx';
    $google_redirect_url    = 'http://localhost/google-login/';
    $google_developer_key   = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    
    ########## MySql details #############
    $db_username = "xxxxxxxxxxxx"; //Database Username
    $db_password = "xxxxxxxxxxxx"; //Database Password
    $hostname = "localhost"; //Mysql Hostname
    $db_name = 'xxxxxxxxx'; //Database Name
    #########################################################
    
    //include google api files
    require_once 'src/Google_Client.php';
    require_once 'src/contrib/Google_Oauth2Service.php';
    
    //start session
    session_start();
    
    $gClient = new Google_Client();
    $gClient->setApplicationName('Login to saaraan.com');
    $gClient->setClientId($google_client_id);
    $gClient->setClientSecret($google_client_secret);
    $gClient->setRedirectUri($google_redirect_url);
    $gClient->setDeveloperKey($google_developer_key);
    
    $google_oauthV2 = new Google_Oauth2Service($gClient);
    
    //If user wish to log out, we just unset Session variable
    if (isset($_REQUEST['reset']))
    {
    unset($_SESSION['token']);
    $gClient->revokeToken();
    header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL));
    }
    
    if (isset($_GET['code']))
    {
    $gClient->authenticate($_GET['code']);
    $_SESSION['token'] = $gClient->getAccessToken();
    header('Location: ' . filter_var($google_redirect_url, FILTER_SANITIZE_URL));
    return;
    }
    
    if (isset($_SESSION['token']))
    {
    $gClient->setAccessToken($_SESSION['token']);
    }
    
    if ($gClient->getAccessToken())
    {
    //Get user details if user is logged in
    $user                 = $google_oauthV2->userinfo->get();
    $user_id              = $user['id'];
    $user_name            = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
    $email                = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
    $profile_url          = filter_var($user['link'], FILTER_VALIDATE_URL);
    $profile_image_url    = filter_var($user['picture'], FILTER_VALIDATE_URL);
    $personMarkup         = "$email<div><img src='$profile_image_url?sz=50'></div>";
    $_SESSION['token']    = $gClient->getAccessToken();
    }
    else
    {
    //get google login url
    $authUrl = $gClient->createAuthUrl();
    }
    
    //HTML page start
    
    echo '<html xmlns="http://www.w3.org/1999/xhtml">';
    echo '<head>';
    echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
    echo '<title>Login with Google</title>';
    echo '</head>';
    echo '<body>';
    echo '<h1>Login with Google</h1>';
    
    if(isset($authUrl)) //user is not logged in, show login button
    {
    echo '<a href="'.$authUrl.'"><img src="images/google-login-button.png" /></a>';
    }
    else // user logged in
    {
    /* connect to mysql */
    $connecDB = mysql_connect($hostname, $db_username, $db_password)or die("Unable to connect to MySQL");
    mysql_select_db($db_name,$connecDB);
    
    //compare user id in our database
    
    $result = mysql_query("SELECT COUNT(google_id) FROM google_users WHERE google_id=$user_id");
    if($result === false) {
    die(mysql_error()); //result is false show db error and exit.
    }
    
    $UserCount = mysql_fetch_array($result);
    
    if($UserCount[0]) //user id exist in database
    {
    echo 'Welcome back '.$user_name.'!';
    }else{ //user is new
    echo 'Hi '.$user_name.', Thanks for Registering!';
    @mysql_query("INSERT INTO google_users (google_id, google_name, google_email, google_link, google_picture_link) VALUES ($user_id, '$user_name','$email','$profile_url','$profile_image_url')");
    }
    
    echo '<br /><a href="'.$profile_url.'" target="_blank"><img src="'.$profile_image_url.'?sz=50" /></a>';
    echo '<br /><a href="?reset=1">Logout</a>';
    
    i//lst all user details
    echo '<pre>';
    print_r($user);
    echo '</pre>';
    }
    
    echo '</body></html>';
    ?>
    									

Comments on this post

Leave a Reply

  • Security Code :


    − 3 = six