MyBB Community Forums

Full Version: Getting session variables from outside mybb folder
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm trying to get information about the logged in mybb user into my main website.  I've searched and it seems like this should work but it doesn't.  This code is in a file in my root directory with triptalk being the mybb directory.

chdir("triptalk/");
define("IN_MYBB", 1);
require("./global.php");
chdir("../");
$c =  $mybb->user['uid'];
echo "User:" . $c;


The result is 

User:0

The same file put into the triptalk directory without the chdir lines shows

User:2

Any ideas would be most appreciated.
I don't have an assured answer, but I think it could be caused by the cookie path setting. MyBB has a cookie setting for URL path. If its value is set to a sub directory, then this sub directory and all its sub directories will be able to read the cookies, but not its parent path. So you'll not get current logged in users information via cookies, meaning the $mybb->user['uid'] value is always set to 0 in MyBB internally.

Also check this tutorial https://community.mybb.com/thread-218011.html , I think it'll be helpful for you. However, changing the cookie path may be undesirable in some rare circumstance such as the parent path is not controlled by you.
What about:
chdir("triptalk/");
define("IN_MYBB", 1);
require("./global.php");
$user = $mybb->user;
chdir("../");
echo "User:" . $user['uid];
you copy any part of the mybb' object in a local variable and then you can use it.