All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Login With Google In PHP

Last Updated : Mar 11, 2024

Login With Google In PHP

In this article we will show you the solution of login with google in PHP, to log in with google in php We need the Google SDK which is the set of software components. After opening the page in the local host, we must download the Google SDK.

This can be done by using Composer. By using the command

composer require google/applicant: "^2.0"

Now first, we have to follow some steps.

  1. First open the Google Cloud platform
  2. go to create a new project
  3. Enter a project name and click on Create Project
  4. open the dashboard and visit API and services
  5. Create an OAuth client ID, then enter the app name, and at the place of the application page enter the local host URL
  6. Also, add your email id then click on save and continue
  7. create an OAuth client id. Then you’ll have the client id and client secret.

Step By Step Guide On Login With Google In PHP :-

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title> login with google in php </title>
    <link rel="stylesheet" href="">
    <h1 style=" color : rgb(78, 182, 78) ; text-align: center;"> Welcome To TalkersCode </h1>
    <h2 style="text-align: center;"> Login With Google in PHP</h2>
require "vendor/autoload.php" ;
$clientID = '//enter your client id here' ;
$clientSecret = '//enter your client secret here' ;
$redirectURL = 'http://localhost/login.php' ;
//creating client request to google
$client = new Google_Client() ;
$client -> setClientId($clientID) ;
$client -> setClientSecret($clientSecret) ;
$client -> setRedirectUrl($redirectURL) ;
$client -> addScope('profile') ;
$client -> addScope('email') ;
if (isset($_GET['code'])) {
$token = $client -> fetchAccessTokenWithAuthCode($_GET['code']) ;
$client -> getAccessToken($token) ;
//getting user profile
$gauth = new Google_Service_Oauth2($client) ;
$google_info = $gauth->userinfo->get() ;
$email = $google_info->email ;
$name = $google_info->name ;
echo "welcome".$name."you are registered using: ".$email ;
else {
    echo "<a hreh='".$client->createAuthUrl()."'> Login With Google </a>"
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now the <head> tag is used to contain information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here add some inline CSS here.
  7. Open <?php tag to write php code inside it
  8. Require tag used to include the autoload.php file inside the vendor folder
  9. Create variables for $clientID, $clientSecret and enter your client id and client secret from the OAuth client page
  10. Another variable for $redirectURL to add your local host page URL here
  11. Now, to create client request to google create the $client variable with the new Google_Client() object.
  12. setClientId() object with the client id variable
  13. setClientSecret() object to set the client secret
  14. setRedirectUrl() to set the redirect URL.
  15. addScope() object to add a profile and email.
  16. Now create an If-Else statement. Between the else statement, create a button with HTML link tag with the createAuthUrl() object.
  17. Now within the if block with the isset() with super global variable $_GET method
  18. Create a variable for $token with the object fetchAccessTokenWithAuthCode()
  19. Get access with the token by using the object getAccessToken()
  20. Now to get user profile, create $gauth with the variable Google_Service_OAuth2()
  21. $google_info with get() the userinfo
  22. Add email and name with $google_info
  23. Using echo to display the error message.

Conclusion :-

At last, here in conclusion, here we can say that with this article’s help, we know how to log in with google in php.

I hope this article on login with google in PHP helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪