MyBB Community Forums

Full Version: Integrating MyBB Accounts with Custom Scripts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Integrating MyBB Accounts with Custom Scripts
Written By : n1tr0b

Introduction
In this tutorial, we're going to tackle MyBB. Since it's one of the most popular free forum softwares in the internet and most likely to have a ton of developers. We're going to learn how to integrate our custom scripts with the MyBB databse.

First of all, if you have separate databases for MyBB and your custom site. I recommend to add the current user in the custom site to the MyBB databse with all privilieges. In that way, we can simultaneously query from the MyBB database with your site's database.

Login Function
Now for the coding, one of the basics in integration is accounts. Now, how can we use those accounts? We use those accounts to login to the site instead of registring a new account. Now that's simple.

Now study the function below how it works.
function mybb_login($username, $password) {
  // Escape the values
  if(function_exists('mysql_real_escape_string') {
    $username = mysql_real_escape_string($username);
    $password = mysql_real_escape_string($password);
  } else {
    $username = addslashes($username);
    $password = addslashes($password);
  }

  // Query the username
  $que = mysql_query("SELECT `password`, `salt` FROM `database`.`mybb_users` WHERE `username` = '{$username}' LIMIT 1");

  // Count the values for checking
  if(!mysql_num_rows($que)) {
    die("Username not found.");
  }

  $row = mysql_fetch_assoc($que);
  // Password validation
  $usalt = $row['salt'];
  $hshed = $row['password']; 
  $input = md5(md5($salt).md5($password));

  if($input != $hshed) {
    die("Incorrect password");
  } else {
  // User logs in here, sessions, cookies, and other assigning of data will be here.
  }
}

Basically, it just logs in the user. With proper functionality and imagination, you might just end up being great. There tons of ways innovating the simple function above. If you have imagination. You're ideas are pretty much limitless.

Usergroup Permissions
Now moving on, Applying permissions to the MyBB usergroup in your custom site is essential. In this way, you can really convince users to upgrade their account or connect their account with their main site account. If you don't have a usergroup table with permissions, I suggest coding one.

First add a column name usergroup in your user tables. Set the type to int(11). Now create a table in your main site database and name it usergroups. The number of columns for permissions is limitless. Take for an example the query below.

CREATE TABLE `usergroups` (
  `gid` INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  `name` VARCHAR(32) NOT NULL,
  `permission_1` INT(1) DEFAULT '0',
  `permission_2` INT(1) DEFAULT '1',

   // And on and on and on.
)

Now, we're going back to our login function. We're now going to add some few lines that will make the permissions work. Since the usergroups in MyBB and in your site are clearly different. Lets auto assign a number which will be queried to the usergroup table which we created a while ago. Check first what gid the MyBB group is on your table. Then assign it to the code below,

// Put this after `User logs in here, sessions...` line.
$uGroup = 1; // Depends which number you assign for the MyBB group. I say 1 here
// Query the group.
$uGroupD = mysql_fetch_assoc(mysql_query("SELECT * FROM `usergroups` WHERE `gid` = '{$uGroup}' LIMIT 1"));
// Now assign it to a session variable
$_SESSION['uGroup'] = $uGroupD;

The assigned data in the variable [i]$_SESSION['uGroup'][/i] is an array. To use it, the code below can explain it.

if($_SESSION['uGroup']['permission_1']) { // This will check if the permission setting is 1
  // Approve access
} else {
  // Deny access
}


Notes
  • If you have suggestions on tutorials about MyBB, feel free to leave a message.
  • If you have problems regarding on MySQL. There will be a tutorial regarding MyBB API



Cheers!,
- n1tr0b, Site Developer SyrGoo Web
hang on this doesn't actually log you in. It doesn't set the myBB cookies or sessions in full.

You are putting in some useful stuff but it's very incomplete. The real value would be in externally logging in.

For instance I am looking at ways I can make a pass through or more, pass along style login. Which allows me to set the cookies based on validation from one source then pushing to the next