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. In this article let us learn to easily register/login users using Google PHP API library and MySQL.
This tutorial requires only one PHP page, all task are performed within the page with support of Google APIs Client Library for PHP. These library files are included in downloadable file at the bottom of this page, also look at their sample code, because this tutorial is based on it.
Run SQL query below to create a new MySQL table called “google_users” using phpMyAdmin. There are five columns in the database table for the tutorial, and you will find that the length of Google profile IDs are too long (21 characters), it is not possible to accommodate these IDs into INT or BIGINT field, hence I have used DECIMAL(21,0), but you can also store them as string in VARCHAR field and create a separate auto increment primary field.
Login and Process Page
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. This is the simplest form of Google login system, if you are familiar with Facebook Login System, then it wont be that hard to understand, because it works pretty much the same.
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.
That’s it, you can download sample files and look at demo I have created for you. I hope this will be helpful in creating Google connect system for your website. Good luck!
Saran Chamling. (He loves the web technology, and enjoys designing, exploring and writing about it @saaraan.com).