How to programmatically log in into phpBB forum?
Asked Answered
G

3

5

I have a forum using phpBB. Now i would like to do something like this from source code:

login("user", "password")

How to do this in phpBB?

Glorify answered 10/11, 2011 at 13:40 Comment(0)
E
8

You will need a script that integrates with the phpBB framework. Something like this should work.

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
?>

Then, look at the $auth->login() function (an example use is in the login_box() function in /includes/functions.php). A simplistic yet incomplete example is:

$result = $auth->login($username, $password); // There are more params but they're optional

if ($result['status'] == LOGIN_SUCCESS)
{
    // Logged in
}
else
{
    // Something went wrong
}
Elwood answered 10/11, 2011 at 14:0 Comment(0)
H
11

First you need to bootstrap for phpBB:

define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './phpBB/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

$user->session_begin();

You'll have to replace the ./phpBB/ part with the relative path to the forum.

To make the user logged in, you have to do:

$result = $user->session_create($user_id, $admin, $autologin, $viewonline);

$admin should probably be false, $autologin and $viewonline depend on what you want.

NOTE: Calling session_create will set the session cookie for the user, so make sure you only call that when the current request is actually serving that user.

Habited answered 10/11, 2011 at 14:1 Comment(4)
This doesn't answer the OP's question, as your solution is not accepting the user's username/password to log them in, but forcing a login of a specfic $user_id.Elwood
Fair enough, I did go a bit too far into the details. I'll leave my answer here anyway, since I put some effort into it. Might help someone someday.Habited
I know that's quite late, but thanks a lot for taking the decision to leave the answer, because that's the only place I've found a solution to how to login users using only the user ID and not the password.Richellericher
For people looking for a way to change the logged in user, you can do this in a similar manner. $user->session_kill(); $user->session_create($newUserID, false, true); $auth->acl($user->data); I'm using this to switch to an anonymous user (userid=1) before my script does some automatic posting, then switching back to the original user (whos userid I have stored before making the switch).Wurster
E
8

You will need a script that integrates with the phpBB framework. Something like this should work.

<?php
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);

// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
?>

Then, look at the $auth->login() function (an example use is in the login_box() function in /includes/functions.php). A simplistic yet incomplete example is:

$result = $auth->login($username, $password); // There are more params but they're optional

if ($result['status'] == LOGIN_SUCCESS)
{
    // Logged in
}
else
{
    // Something went wrong
}
Elwood answered 10/11, 2011 at 14:0 Comment(0)
B
-1

In your PHP script that's crawling through HTML will need to :

  • POST data like if it was filling in the form
  • Get the answer from the server ; probably extract the session's cookies
  • Send those cookies for subsequent requests

You might be interested in using some already-existing library to facilitate that.
For instance, you can take a look at Zend_Http_Client (see also ; the part about Sending Multiple Requests With the Same Client will probably interest you ;-) )


You might also want to take a look at some other questions/answers, like :

Britanybritches answered 10/11, 2011 at 13:45 Comment(3)
Not really sure how this has to do with phpBB?Elwood
The question was: "How to programmatically log in into phpBB forum?". This is a good general answer explaining that.Pinnati
@budwiser - The question was how to login to phpBB, not how to generally log into any remote web application. So, it's not well-suited for phpBB, especially when there's an API available and this answer is talking about crawling HTML and remote authentication. (The OP's question clearly states the need for a local authentication solution)Elwood

© 2022 - 2024 — McMap. All rights reserved.