Reddit login

This tutorial will guild you how to reddit login, at first, you must have reddit account, create a application to receive client id and secret, for more details please see this create reddit app.

After that, write down app_id and app_secret to get start with reddit login.

System requirements

  • PHP 5.4 or higher
  • APACHE 2.4.9 or higher
  • Netbean IDE

Project Structure

 reddit structure

Implement code

Create projecthelper.php

We wrote one tutorial about projecthelper.php, view more at here, if you were already read it, please add this function in file.

  • public static function get_reddit_config() {
    return $config = array(
    'client_id' => 'your-client-id(app_id)',
    'client_secret' => 'client-secret(app_secret)',
    'redirect_uri' => self::get_base_url('reddit-callback.php'),
    'login' => self::get_base_url('reddit-login.php'),
    'session' => 'reddit_access_token'
    );
    }

 

Create reddit-login.php

This file will undertake two main tasks, check access_token in $_SESSION, if existed, send request to get user's profile, the opposite will create $authen_url for the user grant access your applications to connect to their accounts.

- Create $authen_url link

  •  $endpoint = 'https://www.reddit.com/api/v1/authorize?';
    $response_type = 'code';
    $state = 'hellotut';
    $redirect_uri = $config['redirect_uri'];
    $duration = 'permanent';
    $scope = 'identity';
    $auth_url = $endpoint .
    'client_id=' . $config['client_id'] .
    '&response_type=code' .
    '&state=' . $state .
    '&redirect_uri=' . $redirect_uri .
    '&duration=' . $duration .
    '&scope=' . $scope;
    echo '<a href="' . $auth_url . '" >Login with Reddit</a>';

- Send request to get user's profile and print result on screen

  •  $access_token = $_SESSION[$config['session']];
    $endpoint = 'https://oauth.reddit.com/api/v1/me.json';
    $header = array(
    'GET /api/v1/me.json HTTP/1.1',
    'Host oauth.reddit.com',
    'Authorization: Bearer ' . ($access_token['access_token'])
    );
    // get user's data
    $data = projecthelper::make_request($endpoint, $header);
    var_dump($data);

<?php

if (!session_id()) {
session_start();
}

include './projecthelper.php';
$config = projecthelper::get_reddit_config();

if (isset($_SESSION[$config['session']])) {
$access_token = $_SESSION[$config['session']];
$endpoint = 'https://oauth.reddit.com/api/v1/me.json';
$header = array(
'GET /api/v1/me.json HTTP/1.1',
'Host oauth.reddit.com',
'Authorization: Bearer ' . ($access_token['access_token'])
);
// get user's data
$data = projecthelper::make_request($endpoint, $header);
var_dump($data);
} else {
$endpoint = 'https://www.reddit.com/api/v1/authorize?';
$response_type = 'code';
$state = 'hellotut';
$redirect_uri = $config['redirect_uri'];
$duration = 'permanent';
$scope = 'identity';
$auth_url = $endpoint .
'client_id=' . $config['client_id'] .
'&response_type=code' .
'&state=' . $state .
'&redirect_uri=' . $redirect_uri .
'&duration=' . $duration .
'&scope=' . $scope;
echo '<a href="' . $auth_url . '" >Login with Reddit</a>';
}
?>

Create reddit-callback.php

After granted access to your application, user will be redirected to your redirect_uri with authentication code. Then you will make request to get access_token.
(In some cases, your redirect_uri that you provide must match with the one you've described in app's settings, or redirect_uri must begin with https).

+ Check and extract authentication code

  • Send request to get access_token
  •  $code = $_GET['code'];
    $endpoint = 'https://oauth.vk.com/access_token?client_id=' . $config['client_id'] . '&client_secret=' . $config['client_secret'] . '&redirect_uri=' . $config['redirect_uri'] . '&code=' . $code;

    $data = projecthelper::make_request($url);
  • Get result, if there is no error, save the result get into $_SESSION and navigate user to the login page.
  •  $_SESSION[$config['session']] = $data;
    header('Location: ' . $config['login']);
  • If there are errors, print to the screen
  •  print_r($data);

+ If there are errors, print to the screen

  • print_r($_GET);

<?php

if (!session_id()) {
session_start();
}

include './projecthelper.php';
$config = projecthelper::get_vk_config();

if (isset($_GET['code'])) {
$code = $_GET['code'];
$endpoint = 'https://oauth.vk.com/access_token?client_id=' . $config['client_id'] . '&client_secret=' . $config['client_secret'] . '&redirect_uri=' . $config['redirect_uri'] . '&code=' . $code;

$data = projecthelper::make_request($url);

if (is_array($data) && isset($data['access_token'])) {
$_SESSION[$config['session']] = $data;
header('Location: ' . $config['login']);
} else {
print_r($data);
}
} else {
print_r($_GET);
}

After implement code, run project and view result or you can view our example here. If you have questions, don't hesitate, tell us