Coding Examples
This part of the website shall help you by giving you example codes to try out and to use within your own project. There is a great variety of examples. From one-liners to small/great projects.If you have a code example yourself, feel free to contact me, so I can put it into this very example site.Code examples are grouped by topics (such as posts or users).
Examples: Users
Following code examples are associated to the members of a forum.
Code examples include output of memberlist, permission checks, login, logout, registering and more.
createUser
- No example yet
getMembers
- No example yet
getUser
Example 1:
$data = $MyBBI->getUser(2);
print_r($data); // Output of user data
getWhoIsOnline
Example 1:
// Following code shows a short overview of current visitors
$wol = $MyBBI->getWhoIsOnline(true);
echo 'There ';
echo ($wol['count_members'] == 1) ? 'is ' : 'are ';
echo $wol['count_members'].' registered users and '.$wol['count_guests'].' guests on your site<br />';
echo 'Registered users: ';
foreach ($wol['members'] as $member)
{
if ($member_name)
{
echo ', ';
}
$member_name = $member['profilelink'];
echo $member_name;
}
isLoggedIn
Example 1:
echo ($MyBBI->isLoggedIn()) ? 'You are logged in' : 'You are not logged in';
isModerator
Example 1:
echo ($MyBBI->isModerator(2)) ? 'You are moderator of forum #2' : 'You are not moderator of forum #2';
isSuperAdmin
Example 1:
echo ($MyBBI->isSuperAdmin()) ? 'You are super admin' : 'You are not super admin';
Login
Example 1: Simple Auth
$login_status = $MyBBI->login($MyBBI->mybb->input['name'], $MyBBI->mybb->input['password']);
if ($login_status == true)
{
echo 'You were successfully logged in';
}
else
{
echo 'The login routine failed';
}
The given example uses the array $MyBBI->mybb->input. This array contains an already sanitized version of the $_GET and $_POST array. With the help of the MyBB Integrator, you can freely access this sanitized array.
Example 2: Comprehensive Login Auth with Captcha check to prevent Brute-Force
// Auth check when login form is sent (Note: This would also work with Array)
if (isset($MBI->mybb->input['user']) && isset($MBI->mybb->input['pass']))
{
// Captcha Login
if (isset($MBI->mybb->input['captcha_hash']))
{
// So we have the correct captcha hash from the form
$login = $MBI->login($MBI->mybb->input['user'], $MBI->mybb->input['pass'], $MBI->mybb->input['captcha_hash'], $MBI->mybb->input['captcha']);
if (is_array($login))
{
// So we have the correct captcha hash in the form
$captcha_hash = $login['imagehash'];
echo 'Captcha required<br />';
}
}
// Non-Captcha Login
else
{
$login = $MBI->login($MBI->mybb->input['user'], $MBI->mybb->input['pass']);
if (is_array($login))
{
// So we have the correct captcha hash in the form
$captcha_hash = $login['imagehash'];
echo 'Captcha required<br />';
}
}
if ($login === true)
{
echo 'Login successful';
}
else if ($login === false)
{
echo 'Login failed';
}
}
// Let's set up the initial form
echo '<form action="test.php" method="post">';
echo 'Username <input type="text" name="user" value="admin" /><br />';
echo 'Password <input type="password" name="pass" value="Pass" /><br />';
// If the login returns the captcha array, show the captcha in the form
if (is_array($login))
{
echo '<input type="hidden" name="captcha_hash" value="'.$captcha_hash.'" />';
echo $login['captcha'].'<br />';
echo 'Captcha <input type="text" name="captcha" /><br />';
}
echo '<input type="submit" name="login" value="Login" />';
echo '</form>';
Logout
Example 1:
if ($MyBBI->logout())
{
echo 'You were successfully logged out';
}
Register
Example 1:
$info = array(
'username' => 'Testuser',
'password' => 'difficultpassword',
'password2' => 'difficultpassword',
'email' => 'some_address@example.com',
'email2' => 'some_address@example.com',
'hideemail' => 1,
'invisible' => 0,
'receivepms' => 1
);
$register_status = $MyBBI->register($info);
// Array means: registering failed
if (is_array($register_status))
{
echo implode('<br />', $register_status);
}
else
{
echo $register_status;
}
removeUser
Example 1:
// This function call will remove a user with the username being "delete_me"
$remove = $MyBBI->removeUser('delete_me');
if ($remove === true)
{
echo 'User removed!';
}
else
{
echo 'User does not exist';
}
updatePasswordOfUser
Example 1:
$update = $MyBBI->updatePasswordOfUser(1, 'admin');
if ($update === true)
{
echo 'Password updated';
}
else
{
echo $update;
}
updateUser
- No example yet