MyBB Community Forums

Full Version: [Tutorial] Integrating MyBB's Login System With Your Website
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
Thank you! This post helped me a lot! Smile
Ok. 2 questions, 1 problem.

Q1: Is this possible in a .html page? And, does it have to be reffering to .index.php? Can it just reffer to /?

Q2: Is it possible to unlock certain pages if you are logged in? I.e. You log in. You go to a locked page. It shows its contents. You log out. You visit the page again. It shows a denied message, then asks you to log in.

P: I login, it fully works, but the welcome doesnt replace the login form. What have I done wrong?

<?php

if($mybb->user['uid'])
{
    // If the user if logged in, display a welcoming message.
    echo "Welcome back ".$mybb->user['username']."!<br />";
    echo "Thanks for logging in.";
}
else
{
    // If the user is not logged in, display the login form.
    echo "<form action='forum/member.php' method='post'>";
    echo "<input type='hidden' name='action' value='do_login' />";
    echo "<input type='hidden' name='url' value='../test.php' />";
    echo "Username: <input type='text' name='username' maxlength='30' /><br />";
    echo "Password: <input type='password' name='password' /><br />";
    echo "<input type='submit' name='submit' value='Login' />";
    echo "</form>";
}

?>


If you need an example than click here*

*: if you read the php code than you would have noticed that

    echo "<input type='hidden' name='url' value='../test.php' />";

is incorrect. It has been updated on this page.
1) No, this will not work in a .html file. This is PHP code.
2) Yes, it is possible to restrict access to pages unless you are logged in.

It will not display the welcome message probably because you forgot to add this bit of code to the very top of your page:

<?php
chdir("forums/"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
chdir("../");
?>

Make sure you have no conflicting session variables and that you change the paths to reflect the proper path to your forum. Either that or your cookie settings are wrong. I suggest you go back and carefully read through the entire thread again to make sure you did not make a mistake.

In order to restrict access to certain usergroups, you can use something like this (usergroup 4 is typically the administrator usergroup):

if ($mybb->user['usergroup'] == 4) {
//display content if user IS logged in.
} else {
//display content if user is NOT logged in.
}

I recommend using usergroups. However, if you wish to restrict access to simply anyone who logs in, use something like:

if($mybb->user['uid']) {
//display content if user IS logged in.
} else {
//display content if user is NOT logged in.
}

Hope that helps. Please keep in mind I do not have a lot of time to check for responses to this thread and therefore I cannot guarantee I will be able to answer any further questions in a timely manner.

Take care.
Hi. I have managed to check the login cookie, and get the login form to work, on a test page, but I'm having trouble applying it to a template.

<?

// direct access redirect
if ($_SERVER["SCRIPT_NAME"] === "/admin/template.php")
{
	header("Location: /");
}

// start the session
session_start();

// write the header
function template_header($title)
{
	// MyBB login file
	chdir("/home/a6550792/public_html/forum/");
	define("IN_MYBB", 1);
	require("global.php");
	$uLogOn = ($mybb -> user['uid']);
	$uGroup = ($mybb -> user['usergroup']);
	$uAdmin = ($uGroup === 4);
	chdir("/home/a6550792/public_html".substr($_SERVER["SCRIPT_NAME"], 0, -9));
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<!-- header code - not needed -->
	</head>
	<body>
		<div id="header">
			<div id="menu">
				<ul>
					<!-- other menu items -->
<?
	// if (session_is_registered("account"))
	if ($mybb -> user['uid'])
	{
?>
					<li>
						<a href="/account/" accesskey="u" title="Go to <? print ($mybb -> user['username']); ?>'s account page... (Alt+U)">Account</a>
					</li>
					<li>
						<a href="/account/logout/" accesskey="l" title="Logout as <? print ($mybb -> user['username']); ?>... (Alt+L)">Logout</a>
					</li>
<?
	}
	else
	{
?>
					<li>
						<a href="/account/login/" accesskey="l" title="Login to your account... (Alt+L)">Login</a>
					</li>
					<li>
						<a href="/account/register/" accesskey="r" title="Register for a new account... (Alt+R)">Register</a>
					</li>
<?
	}
?>
				</ul>
			</div>
		</div>
		<div id="page">
			<div id="bg">
				<div id="content">
<?
}

// additional functions for other parts of the site are further down

?>

I always see the site as if I'm logged out. Is there any reason why?



Edit: never mind, I've made myself a workaround.
Thaaanks a lot for this Smile
All,

Can someone help me out? I've installed MyBB many times in the past, so I am by no means a newbie with this stuff. But the craziest thing is happening, and I can't figure out what it is! Here's what I have so far:
<?
chdir("bb/"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
chdir("../");
?>
<html>
  <head>
  <LINK href="2010.css" rel="stylesheet" type="text/css">
  <title>Login Page</title>
  </head>
  <body margin=0 cellpadding=0 cellspacing=0>
  	<div id="front_body_wrapper">
		<table class="maintable" border=0 cellspacing=0 cellpadding=0>
			<tr>
				<td>
					<?
					if($mybb->user['uid'])
					{
					    // If the user if logged in, display a welcoming message.
					    echo "Welcome back ".$mybb->user['username']."!<br />";
					    echo "Thanks for logging in.";
					}
					else
					{
						echo "here->".$mybb->user['username'];
					    // If the users is not logged in, display the login form.
					    echo "<form action='bb/member.php' method='post'>";
					    echo "<input type='hidden' name='action' value='do_login' />";
					    echo "<input type='hidden' name='url' value='../land.php' />";
					    echo "Username: <input type='text' name='username' maxlength='30' /><br />";
					    echo "Password: <input type='password' name='password' /><br />";
					    echo "<input type='submit' name='submit' value='Login' />";
					    echo "</form>";
					}
					?>
				</td>
			</tr>
		</table>
	</div>
  </body>
</html>

No problem, right? The form itself is straight from the tutorial, and it works when it submits to member.php.

Now notice when I submit the form, it redirects to a file called "land.php", which is this, for simplicity:
<?
chdir("bb/"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
echo "logged in as:".$mybb->user['username'];
?>

Ok. So here's the thing. When member.php redirects to "land.php", all I get for an output is:

logged in as:

In other words, there is no result. And $mybb->user['uid'] outputs "0" when I try to echo that.

Also, when I change the form redirect value to "bb/index.php" (which is the actual forum), the forum itself recognizes me as logged in and says, "Welcome!"

I can even log in using the external form, and watch it redirect to itself (as the index.php code that you see above), but it just gives me the login box again since the $mybb object is empty. But if I then manually type my site's URL pointing directly to the forum, the forum still recognizes me as logged in.

1. So it is safe to say that there's no problem with the forum itself, because I can log in and out without any problem.
2. I know there are no cookie issues either, because I can log out and my browser will refresh and give me a new login request.
3. I know it's also not the cookies because when I use the external login page that I pasted above, I can then go straight to the forums by typing the direct URL and I am recognized as logged in, which means my cookie is being properly read.

(I also tried deleting cache and cookies after each and every attempt, too. And I tried this on multiple browsers--IE, Firefox, Chrome)

It has to be a server setting or something, wouldn't you think? Why, after submitting my form data to member.php, would $mybb->user['username'] and ['uid] return empty???

Is there possibly another initialization setting or file that I need to include on my login page?

Thanks for any help!
Seng
Ugh.. I was afraid this would be the answer! *silence*

Since I posted the last message, I "hacked" a solution by creating two session variables containing the "uid" and "username" values, which I could then access from another web page.

But this only patches the problem; it does not fix the underlying issue of why my $mybb->user objects are turning up null while other $mybb-> objects from included files are not.

Naturally, I do not want to have to patch the source code every time I upgrade versions, so any help would be much appreciated!

Thanks,
Seng
How would I display
<img src='USER AVATAR' />
Hello Seng,

A couple things I noticed. First, you need to remove the echo "here->" line from this code block:

                    else
                    {
                        echo "here->".$mybb->user['username'];
                        // If the users is not logged in, display the login form.
                        echo "<form action='bb/member.php' method='post'>";
                        echo "<input type='hidden' name='action' value='do_login' />";
                        echo "<input type='hidden' name='url' value='../land.php' />";
                        echo "Username: <input type='text' name='username' maxlength='30' /><br />";
                        echo "Password: <input type='password' name='password' /><br />";
                        echo "<input type='submit' name='submit' value='Login' />";
                        echo "</form>";
                    }

Secondly, land.php should look like this:

<?php

chdir("bb/"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
chdir("../");

if($mybb->user['uid'])
{
     echo "logged in as: ".$mybb->user['username'];
}

?>

In case you are wondering, if you are interested in restricting access to certain pages, please see my post earlier in this thread located [here]. Hope that helps.

Take care.
(2010-04-05, 10:26 PM)Prentice Wrote: [ -> ]How would I display
<img src='USER AVATAR' />

Hello Prentice,

You would need to do something like:

<?php
$dimensions = explode('|', $mybb->user['avatardimensions']);

echo "<img src=\"".$mybb->user['avatar']."\" alt=\"".$mybb->user['username']."'s Avatar\" style=\"width: ".$dimensions[0]."; height: ".$dimensions[1].";\" />";
?>

Hope that helps.

Take care.
Hi,
Thanks for your reply... Didn't work though
<?php
chdir("forums/"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
chdir("../");
?>
<?php
$dimensions = explode('|', $mybb->user['avatardimensions']);
if($mybb->user['uid'])
{
// The user is logged in, say Hi
echo "<div id='usertop'>
	<img src=\"".$mybb->user['avatar']."\" alt=\"".$mybb->user['username']."'s Avatar\" style=\"width: ".$dimensions[0]."; height: ".$dimensions[1].";\" />
	<ul>
		<li><img src='images/icons/pencil.png' />User CP</li>
		<li><img src='images/icons/camera.png' />Avatar Creator</li>
		<li>
	</ul>
</div>
<div id='Navbar'>
	<p>
		<img src='images/icons/error.png' title='House img' /> Hey ".$mybb->user['username'].", Thanks for registering!
	</p>
</div>";
}
else
{
echo "<div id='Navbar'>
	<p>
		<img src='images/icons/error.png' title='House img' /> Hey Guest, Thanks for register or login!<form action='forums/member.php' method='post'>";
		    echo "<form action='forums/member.php' method='post'>";
    echo "<input type='hidden' name='action' value='do_login' />";
    echo "<input type='hidden' name='url' value='../index.php' />";
    echo "Username: <input type='text' name='username' maxlength='30' /><br />";
    echo "Password: <input type='password' name='password' /><br />";
    echo "<input type='submit' name='submit' value='Login' />";
    echo "</form>";
	echo" </p>
</div>";
}
?>
Just displays then broken image icon and when I go to look at the URL it is directed towards my homepage?
Pages: 1 2 3 4 5