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!