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.
- First open the Google Cloud platform
- go to create a new project
- Enter a project name and click on Create Project
- open the dashboard and visit API and services
- Create an OAuth client ID, then enter the app name, and at the place of the application page enter the local host URL
- Also, add your email id then click on save and continue
- 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"> <head> <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="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css"> </head> <body> <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> </body> </html> <?php 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>" } ?>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here add some inline CSS here.
- Open <?php tag to write php code inside it
- Require tag used to include the autoload.php file inside the vendor folder
- Create variables for $clientID, $clientSecret and enter your client id and client secret from the OAuth client page
- Another variable for $redirectURL to add your local host page URL here
- Now, to create client request to google create the $client variable with the new Google_Client() object.
- setClientId() object with the client id variable
- setClientSecret() object to set the client secret
- setRedirectUrl() to set the redirect URL.
- addScope() object to add a profile and email.
- Now create an If-Else statement. Between the else statement, create a button with HTML link tag with the createAuthUrl() object.
- Now within the if block with the isset() with super global variable $_GET method
- Create a variable for $token with the object fetchAccessTokenWithAuthCode()
- Get access with the token by using the object getAccessToken()
- Now to get user profile, create $gauth with the variable Google_Service_OAuth2()
- $google_info with get() the userinfo
- Add email and name with $google_info
- 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.