0

I am using PHPBB3 3.0.12 and I have a script that does an auto-login as described here:

phpBB3 auto-login

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?

Community
  • 1
  • 1
Adam
  • 25,960
  • 22
  • 158
  • 247
  • Show your full code – Nikhil Vaghela Oct 26 '16 at 12:39
  • 1
    ___My Guess:___ Because the LIVE server will have had **error reporting turned off**, it was happening all the time, you just never looked at the **LIVE Error logs**. But as XAMPP is a dev tool, this has error reporting turned on – RiggsFolly Oct 26 '16 at 12:42
  • @RiggsFolly but its a Fatal error. My live server stops when an fatal error happens. – Adam Oct 26 '16 at 12:43
  • @CD001 yeah very strange that this method does not exists. But its called in the famous PHPBB3 LIBRARY which is used by thousand of users. – Adam Oct 26 '16 at 12:45
  • ^ yeah, sorry, deleted my comment - read the docs ... seems that phpBB's `$db` is pointing at `mysqli` ... which seems *off*, it should be abstracted: https://wiki.phpbb.com/Database_Abstraction_Layer and https://wiki.phpbb.com/Queries_in_phpBB3 – CD001 Oct 26 '16 at 12:46
  • https://www.phpbb.com/community/viewtopic.php?f=46&t=1267665 – Patrick Q Oct 26 '16 at 12:49
  • @PatrickQ Thank you for the link! I was able now to produce a working code and a non-working code because of it! – Adam Oct 27 '16 at 09:48

0 Answers0