MyBB Community Forums
Is there anyway to pull the user's username to use outside the /forums/ directory - Printable Version

+- MyBB Community Forums (https://community.mybb.com)
+-- Forum: 1.8 Support (https://community.mybb.com/forum-175.html)
+--- Forum: General Support (https://community.mybb.com/forum-176.html)
+--- Thread: Is there anyway to pull the user's username to use outside the /forums/ directory (/thread-206286.html)



Is there anyway to pull the user's username to use outside the /forums/ directory - contrad2 - 2016-11-29

I installed myBB to supplement my website, not to take it over. I'm currently having trouble pulling the logged in users' username so I can assign it to session data: $_SESSION['username'];
Is there anyway that this is possible? This is to happen outside of the forums/ directory but I suppose include/require would allow that, assuming pulling this data is possible.

I'm essentially trying to set session data, site wide, upon successful mybb login with $_SESSION['username'] = to the mybb login username.


RE: Is there anyway to pull the user's username to use outside the /forums/ directory - dragonexpert - 2016-11-30

What you will need to do is include the global.php file from the directory your forum is installed in. Do note that you will need to define the constant IN_MYBB as the value 1 or it will give you an error.

<?php
define("IN_MYBB", 1);
require_once "path_to_global.php";
if($mybb->user['uid']) {
$_SESSION['username'] = $mybb->user['username'];
}
else {
// Not logged in user.
}



RE: Is there anyway to pull the user's username to use outside the /forums/ directory - contrad2 - 2016-12-01

(2016-11-30, 09:01 PM)dragonexpert Wrote: What you will need to do is include the global.php file from the directory your forum is installed in.  Do note that you will need to define the constant IN_MYBB as the value 1 or it will give you an error.

<?php
define("IN_MYBB", 1);
require_once "path_to_global.php";
if($mybb->user['uid']) {
$_SESSION['username'] = $mybb->user['username'];
}
else {
// Not logged in user.
}


Thanks for the reply. It's not setting the session, even if I'm currently logged into the forums. Does this need to be executed from a myBB template of some kind? I currently have it in it's own php block on a separate page. It's executing the code as the else result is firing, but the session isn't being set.


RE: Is there anyway to pull the user's username to use outside the /forums/ directory - Lunorian - 2016-12-01

(2016-12-01, 12:10 AM)contrad2 Wrote:
(2016-11-30, 09:01 PM)dragonexpert Wrote: What you will need to do is include the global.php file from the directory your forum is installed in.  Do note that you will need to define the constant IN_MYBB as the value 1 or it will give you an error.

<?php
define("IN_MYBB", 1);
require_once "path_to_global.php";
if($mybb->user['uid']) {
$_SESSION['username'] = $mybb->user['username'];
}
else {
// Not logged in user.
}


Thanks for the reply. It's not setting the session, even if I'm currently logged into the forums. Does this need to be executed from a myBB template of some kind? I currently have it in it's own php block on a separate page. It's executing the code as the else result is firing, but the session isn't being set.

This does not utilize the template system. Did you adjust,
require_once "path_to_global.php";
to include the actual path?

Something like,
<?php
define("IN_MYBB", 1);
require_once "./forums/global.php";
if($mybb->user['uid']) {
$_SESSION['username'] = $mybb->user['username'];
}
else {
// Not logged in user.
} 
might be a better example for someone less experienced in PHP.

Please let us know the exact code you are using to set the session and please let us know if you are logged into mybb at the time.


RE: Is there anyway to pull the user's username to use outside the /forums/ directory - dragonexpert - 2016-12-01

You don't need to execute it from a template at all. I"m not too familiar with using the superglobal $_SESSION. Try using session_start() on the line after require_once. You can just use the $mybb->user object though for your needs most likely as it contains the uid, username, email, and other information about a user.