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
I've noticed the other thread regarding this topic has become out-dated (no longer works) and cluttered with information not relevant to the tutorial. So I've decided to re-create it for the users of this community.

Using a Top Level Domain

Let us assume that your domain name is www.mydomain.com and that your website structure looks like this:

Quote:public_html/index.php
public_html/forums/global.php

The first thing you want to do is login to the Administration Control Panel of your forums. Navigate to Configuration -> Settings -> General Configuration and scroll down. Look for the cookies section and enter:

Quote:Cooke Domain: .mydomain.com
Cookie Path: /
Cookie Prefix: (leave blank)

Open up your websites index.php file and before any code (literally the very first line), that includes any php or your doctype, place this code:

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

Now choose the location where you want your login form to appear. Copy and paste this code:

<?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='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>";
}

?>

Please take note of the following line. Editing it's value will redirect your users where you want them to be taken after they login.

Redirect you back to your websites index page:
echo "<input type='hidden' name='url' value='../index.php' />";

Redirect you to your forum:
echo "<input type='hidden' name='url' value='index.php' />";

Which will produce (in this case my username is eRott obviously):

Quote:Welcome back eRott!
Thanks for logging in.

It is important that you update the following lines to reflect the appropriate installation directory (where you installed your forums) if it is different then the example location. Remember, ../ will always take you back one level or 'group' of folders. The idea is to change the working directory to that of the forums, include the global.php file as well as define the named constant then change back to the directory your website is in.

chdir('forums/');

chdir('../');

echo "<form action='forums/member.php' method='post'>";



Using a Sub Level Domain

If you are using a subdomain, then you will need to edit your cookie settings as they are different from above. Let us assume that your domain name is subdomain.somehost.com and your website structure looks like this:

Quote:public_html/index.php
public_html/forums/global.php

Your cookie settings should then look something like:

Quote:Cooke Domain: .subdomain.somehost.com
Cookie Path: /forums/
Cookie Prefix: (leave blank)

I am not entirely sure if those are the correct cookie settings for sub-domains as I do not use a sub-domain and therefore cannot test it out. If someone would like to try it out and let me know, that would be great.



Keywords (help future users find this thread easier): tutorial, mybb, login, system, integrate, website, form

Take care.
After changing the cookie path, i get the admin login on every page in the admin cp. The forum itself is working fine.

Edit: This problem only happened on Opera. On Firefox everything works fine.
(2009-11-29, 08:11 PM)eRott Wrote: [ -> ]However with 40 pages of users complaining it no longer works...

(2009-11-29, 08:13 PM)MattRogowski Wrote: [ -> ]But why does yours work if that one doesn't?? I can't see any major differences between the two at all, an the login form is copied and pasted.

Perhaps the "40 pages of users complaining it no longer works"... actually = "40 pages of it *never* did really work right at all" (in every browser) Toungue


Careful!!:

If anyone has a spare few minutes, please test this in IE.
When changing the MyBB cookiepath to...
Cookie Path: /
...it tends to work fine in Firefox, but you can't logout in IE.

Thanks
Sorry guys,

It never really occurred to be to bother testing this in Internet Explorer or Opera. I never use them because they are in my opinion, terrible browsers.

But for the sake of compatibility, I have gone ahead and tested them at your requests. I was unable to reproduce the errors your experiencing. It works fine in Firefox (3.5.5), Chrome (3.0.195.33), Opera (10.00 build 1750), and Internet Explorer (8.0.6001.18828).

I'm afraid I am unable to be of much more assistance.



Edit: I believe I may have found a solution to your problems regarding Opera and Internet Explorer. I have gone ahead and updated the code in my first post. Basically, I want you to change back to the directory you were working from after including the global.php file from the forum directory.

So change:
<?php
chdir('forums/');
define("IN_MYBB", 1);
require './global.php';
?>

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



Be sure to edit in the appropriate values. Another example, if your website structure looks like:

Quote:public_html/index.php
public_html/site/forums/global.php

And you are including the login form on index.php, then you would want to use something like:

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

Hope that solves it.
IE isn't my choice either, but it's nice to know when things are going to work.
I'll try it soon, *thank you.*
It works fine now but you have to delete the browser's cookies before trying this out Wink.
MyBB seems to store the files into your browser as cookies or something, forcing you to clear your cookies when you update some pages.

But anyway...
(2009-12-09, 10:35 AM)AussieJay Wrote: [ -> ]MyBB seems to store the files into your browser as cookies or something, forcing you to clear your cookies when you update some pages.

But anyway...

I'm afraid I don't quite understand what it is you are asking or trying to say AussieJay.

You cannot store files inside of cookies. A cookie is just a small text file stored on your computer by your browser. Cookies generally only consist of small bits of information. Such as user preferences or session identifiers. Refreshing your browser or updating a page won't delete a cookie.

Feel free to take a look at [this particular article] from the MyBB Wiki.

Take care.
eRott, It's working for me in both FF and IE, thanks a lot. Smile
(2009-12-14, 06:20 AM)seeker Wrote: [ -> ]eRott, It's working for me in both FF and IE, thanks a lot. Smile

You are most welcome.
Pages: 1 2 3 4 5