In this example, I will tell you how to login or signup with google account in PHP application.
Google login api allow user to sign into the website without filling signup form manually.
It helps to increase subscribers on the website because user can easily log in the website using their google account without any registration.
In this example, I will provide you simple step to authenticate users with their google account.
For this example, I will create a users table in MySQL database and include the Google API library in the form.
You must have Google Client ID to authenticate with Google account and you can get this key from Google API Console.
You can create any new project on console or you can go with existing project.Step 1: Create Users Table
In this first step, I will create a users table for this example.
CREATE TABLE IF NOT EXISTS `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `password` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, `social_id` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci AUTO_INCREMENT=24 ;Step 2: Create Login View File
In this second step, I will create HTML view having Google login button.login.php Step 3: Create PHP file to authenticate user
In this step, I will create PHP file and confiure MySQL database.social_login.php
In above code, I create database object first and validate if email is exist or not in users table, If it exist then I update the social id generated from Google otherwise I will insert new record of a user in users table.Step 4: Create User Dashboard Page
In this step, I will create dashboard.php file and you will be redirecting on this page after successfully authenticated with Google account.dashboard.php