I am using PHPBB3 3.0.12 and I have a script that does an auto-login as described here:
Here is my script
<?php
login();
function login(){
define('IN_PHPBB', true);
$phpbb_root_path = pathNLP() . 'forum/'; //the path to your phpbb relative to this script
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . "common.php"); ////the path to your phpbb relative to this script
include($phpbb_root_path . "includes/functions_user.php");
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
$username = "testuser"; $password = "1";
if(isset($username) && isset($password))
{
$result=$auth->login($username, $password, true);
if ($result['status'] == LOGIN_SUCCESS) {
echo "You're logged in";
}
}
}
This used to work on my webhoster. I am trying this script currently on my local PC with XAMPP but I get the following error:
Fatal error: Call to undefined method mysqli::sql_query() in /home/adam/www/mysite/forum/includes/cache.php on line 51
and indeed, on line 51 in cache.php I find the command sql_query:
global $db
// ...
$sql = 'SELECT config_name, config_value, is_dynamic
FROM ' . CONFIG_TABLE;
$result = $db->sql_query($sql); //<--line 51
I think the problem is the global $db line, I guess $db should not be a mysqli object, since it does not have a sql_query method. According to a quote from https://www.phpbb.com/community/viewtopic.php?f=46&t=1267665 (Thanks to Patrik Q for the link) someone said:
There is a design error in phpbb3. They use global $db EVERYWHERE instead of using a singleton for the db object. The problem in my case was that i was putting this code inside of a class. [...] in my case i just moved that code to outside the class and copied the vars in the constructor
Because of this quote I tried to call the script not inside a function - and to my surprise it worked! However, I need to call it from a function. I do not understand why global $db makes problems when putting it in a function. What can I do to make the script work inside a function? Here is the working code:
<?php
define('IN_PHPBB', true);
$phpbb_root_path = pathNLP() . 'forum/'; //the path to your phpbb relative to this script
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . "common.php"); ////the path to your phpbb relative to this script
include($phpbb_root_path . "includes/functions_user.php");
// Start session management
$user->session_begin();
$auth->acl($user->data);
$user->setup();
$username = "testuser"; $password = "1";
if(isset($username) && isset($password))
{
$result=$auth->login($username, $password, true);
if ($result['status'] == LOGIN_SUCCESS) {
echo "You're logged in";
}
}
What can I do to make this script available in a method?