Implement Google Login with PHP
To implement Google Login with PHP, you will utilize Google’s OAuth API to authenticate users. Below, I’ll outline the steps along with corresponding PHP code:
Prerequisites:
– A Google account
– Basic knowledge of PHP and MySQL
Step 1: Create a Google API Console Project
1. Go to the [Google API Console](https://console.developers.google.com/) and create a new project.
2. Enable the Google+ API for your project.
Step 2: Retrieve the Client ID and Secret Key
1. Once your project is created, go to the “Credentials” tab.
2. Create credentials and select “OAuth client ID”.
3. Choose “Web application” as the application type.
4. Set the redirect URI to your callback URL.
Step 3: Creating the Login Page with HTML (login.html)
<!-- login.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Login with Google</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="login-container">
<h2>Login with Google</h2>
<a href="login.php" class="login-button">Login with Google</a>
</div>
</body>
</html>
Step 4: Implementing the Google OAuth API with PHP and cURL (login.php)
<?php
// login.php
session_start();
// Google API credentials
$clientID = 'YOUR_CLIENT_ID';
$clientSecret = 'YOUR_CLIENT_SECRET';
$redirectURI = 'YOUR_REDIRECT_URI';
// Generate OAuth URL
$authURL = 'https://accounts.google.com/o/oauth2/auth';
$authURL .= '?client_id=' . $clientID;
$authURL .= '&redirect_uri=' . urlencode($redirectURI);
$authURL .= '&scope=email profile';
$authURL .= '&response_type=code';
$authURL .= '&access_type=offline';
// Redirect to Google login page
header('Location: ' . $authURL);
exit();
?>
Step 5: Validate Access Token and Obtain User Info (callback.php)
<?php
// callback.php
session_start();
if (isset($_GET['code'])) {
$accessToken = getAccessToken($_GET['code']);
if ($accessToken) {
$userInfo = getUserInfo($accessToken);
if ($userInfo) {
$_SESSION['user'] = $userInfo;
header('Location: profile.php');
exit();
}
}
}
function getAccessToken($code) {
// Use cURL to send POST request to exchange code for access token
// Implement cURL request and error handling here
return $accessToken;
}
function getUserInfo($accessToken) {
// Use cURL to send GET request to retrieve user info
// Implement cURL request and error handling here
return $userInfo;
}
?>
Step 6: Creating the Profile Page with PHP and HTML (profile.php)
<?php
// profile.php
session_start();
if (!isset($_SESSION['user'])) {
header('Location: login.html');
exit();
}
$userInfo = $_SESSION['user'];
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>User Profile</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="profile-container">
<h2>Welcome, <?php echo $userInfo['name']; ?></h2>
<p>Email: <?php echo $userInfo['email']; ?></p>
<a href="logout.php">Logout</a>
</div>
</body>
</html>
Step 7: Creating the Logout Script (logout.php)
<?php
// logout.php
session_start();
session_unset();
session_destroy();
header('Location: login.html');
exit();
?>
Explanation:
– The `login.html` page contains a link to initiate the Google login process.
– When the user clicks the link, they are redirected to `login.php`, which initiates the OAuth process and redirects them to Google’s login page.
– After successful authentication, Google redirects the user back to `callback.php`, where the access token is exchanged for user information.
– If the user information is successfully retrieved, it is stored in a session variable and the user is redirected to the `profile.php` page.
– The `profile.php` page displays the user’s profile information and provides a logout link.
– Clicking the logout link triggers the `logout.php` script, which destroys the session and redirects the user back to the login page.
This implementation allows users to login using their Google account and access their profile information. Ensure that you replace `’YOUR_CLIENT_ID‘`, `’YOUR_CLIENT_SECRET‘`, and `’YOUR_REDIRECT_URI‘` with your actual Google API credentials and redirect URI. Additionally, implement error handling and security measures as needed.