Login with GitHub OAuth using PHP
Wall Script
Wall Script
Monday, February 10, 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.

Login with Google Account OAuth.


Download Script     Live Demo

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.
Login with Github OAuth PHP.

Step 2: Create Github Application
Submit your website details.
Login with Github OAuth PHP

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

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
Arun Kumar Sekar
Arun Kumar Sekar

Engineer, Plugin Expert
Chennai, INDIA

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);
?>
web notification

14 comments:

  1. think you... bravo..

    ReplyDelete
  2. Excellent entry! I'm been looking for topics as interesting as this.

    ReplyDelete
  3. Very Good i learnt many things!

    ReplyDelete
  4. Excelent! AWESOME! thank you very much!

    ReplyDelete
  5. Very nice, except for lack of sanitizing db input. Prepared statements FTW

    ReplyDelete
  6. $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;
    Why not data insert ?

    ReplyDelete
  7. Where is the "Github_Lib/githubApi.php" file?

    ReplyDelete
  8. Pls kindly provide me that githubApi.php and githubConfig.php files. I will be very helpfull. Thank u

    ReplyDelete
  9. Hello the script is not inserting any data anybody that can help?

    ReplyDelete

mailxengine Youtueb channel
Make in India
X