Friday, August 15, 2014

phpBB API - Auto Login a User Without a Password

phpBB Logo


In my previous post, phpBB API - Logging In And Logging Out a User, I showed how to login a user to phpBB forum using their username and password.

However in some cases, you may want to login a user without having or knowing their password.

phpBB API provides a method to auto login users by just knowing their phpBB ID.
This can be useful if you have a website connected to phpBB forum and you're only storing the usernames or IDs in that database. Or if you are implementing a Facebook Login Button on your website and you want to synchronize the session with your phpBB forum.

<?php
define('IN_PHPBB', true);//Must be defined
$phpbb_root_path = '../phpBB3/';//your forum directory location
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once($phpbb_root_path . 'common.' . $phpEx);

function phpbbAutoLogin($id)
{
    global $phpbb_root_path, $phpEx, $user;

    $user->session_kill(false);//Logout the User - For testing
    $user->session_begin(); //Start Session
    $user->session_create($id); //Create Session

    //Check if User has successfully Logged in
    if($user->data['is_registered']==1 && $user->data['user_type'] != USER_INACTIVE && $user->data['user_type'] != USER_IGNORE)
    {
        echo  $user->data['username'] . ' has logged in';
    }
    else
    {
        echo 'Error Logging In';
    }
}

//Auto Login User with phpBB User ID 52
phpbbAutoLogin(52);
?>

Questions? Please let me know!

Monday, August 4, 2014

phpBB API - Logging In And Logging Out a User


phpbb Logo

What is PHPBB3

phpBB is an open source forum software that is very easy to use and build communities. Most importantly it's totally free.

phpBB3 provides an API that can be used to automatically login a user from a PHP script.

Although most of the phpBB API functions are documented, however their Wiki lacks examples and can be a little bit difficult to get started.

If you would like to synchronize your website with a phpBB forum, you can use the phpBB sessions to authenticate users.

Include The Following

define('IN_PHPBB', true); //To be allowed to access the API files
$phpbb_root_path = '../phpBB3/'; //phpbb forum path
$phpEx = substr(strrchr(__FILE__, '.'), 1);
require_once($phpbb_root_path . 'common.' . $phpEx);
require_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);

Logging in a User

function phpbbLogin($username, $password)
{
    global $phpbb_root_path, $phpEx, $user, $auth;

    $user->session_begin();
    $auth->acl($user->data);

    $auth->login($username, $password, true);

    if($user->data['is_registered']==1 && $user->data['user_type'] != USER_INACTIVE && $user->data['user_type'] != USER_IGNORE)
    {
        echo 'User is logged in';
    }
}

Logging out a User

function phpbbLogout()
{
    global $phpbb_root_path, $phpEx, $user;
    $user->session_kill(false);
    echo 'User is logged out';
}


Questions? Please ask!