Thursday 13 March 2014

Login with GitHub OAuth using PHP

Nowadays GitHub.com(web based hosting service) is the most import part in developer’s life. In this I want to discuss how to implement GitHub OAuth login system for your web project, this is very simple adopt and sure it will helps you to increase your web project registrations. Please check my previous posts for Google, Facebook and Instagram OAuth login system scripts.




Database
Sample database design 
CREATE TABLE users
(
id INT PRIMARY KEY AUTO_INCREMENT,
email VARCHAR(50) UNIQUE,
fullname VARCHAR(100),
company VARCHAR(50),
blog VARCHAR(50),
location VARCHAR(50),
github_id VARCHAR(50),
github_username VARCHAR(50),
profile_image TEXT,
github_url TEXT
)


Step 1: Github New Application
Register new application at click here.


Step 2: Create Github Application
Submit your website details.


Step 3: Github OAuth Detials
Here the application OAuth client ID and client secret.


The script contains one folder called Github_Lib with PHP files.
Github_Lib
-- githubConfig.php //Github app configuration file.
-- githubApi.php //Github OAuth API.
db.php //Database connection 
github_login.php //Githut Login 
index.php
home.php
logout.php

GitHub API Developer


githubConfig.php
Here modify your application client id, client secret, redirect url and app name values.
<?php
$config = array(
'client_id' => 'Your Client ID',
'client_secret' => 'Your Client Secret',
'redirect_url' => 'http://www.yourwebsite.com/github_login.php',
'app_name' => 'Your Application Name'
);
?>

github_login.php
GitHub OAuth login system, just include the file in index.php
<?php
require_once('Github_Lib/githubConfig.php');
require_once('Github_Lib/githubApi.php');
if($_SERVER['REQUEST_METHOD'] == 'GET')
{
if(isset($_GET['code']))
{
$git = new githubApi($config);
$git->getUserDetails();
$_SESSION['github_data']=$git->getAllUserDetails();
header("location: home.php");
}
else
{
$url = "https://github.com/login/oauth/authorize?client_id=".$config['client_id']."&redirect_uri=".$config['redirect_url']."&scope=user";
header("Location: $url");
}
}
?>

index.php
Calling Github login system. 
<?php
session_start();
if (isset($_SESSION['github_data'])) {
// Redirection to application home page.
header("location: home.php");
}
//HTML Code
<a href="github_login.php">Login with Github</a>
?>

home.php
Contains PHP code inserting GitHub user session details into users table. 
<?php
session_start();
include('db.php'); //Database Connection.
if (!isset($_SESSION['github_data'])) {
// Redirection to application index page.
header("location: index.php");
}
else
{
$userdata=$_SESSION['github_data'];
$email = $userdata->email;
$fullName = $userdata->name;
$company = $userdata->company;
$blog = $userdata->blog;
$location = $userdata->location;
$github_id = $userdata->id;
$github_username = $userdata->login;
$profile_image = $userdata->avatar_url;
$github_url = $userdata->url;

$q=mysqli_query($connection,"SELECT id FROM github_users WHERE email='$email'");
if(mysqli_num_rows($c) == 0)
{
$count=mysqli_query($connection,"INSERT INTO github_users(email,fullname,company,blog,location,github_id,github_username,profile_image,github_url) VALUES('$email','$fullName','$company','$blog','$location','$github_id','$github_username','$profile_image','$github_url')");
}
print_r($userdata); // Full data
echo "<a href='logout.php'>Logout</a>";
}
?>

logout.php
Unsetting the GitHub user session data.
<?php
session_start();
unset($_SESSION['github_data']);
header("Location: index.php");
?>

db.php
Database configuration file, modify username, password and database values. 
<?php
define('DB_SERVER', 'localhost');
define('DB_USERNAME', 'username');
define('DB_PASSWORD', 'password');
define('DB_DATABASE', 'database');
$connection = @mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
?>

No comments:

Post a Comment