MyBB Community Forums

Full Version: Cannot grab correct UID from hook
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to write a plugin that will set an identical session cookie for another area of my website after a successful MyBB login.  Everything works exactly how I want, except it will not grab the correct UID from the session; instead it is returning "0".


Here is the relevant section of my plugin code:

function myplugin_datahandler_login_complete_end()

{
global $mybb, $db, $session;

$cookie_name = "my_session";
$sessionid = $session->sid;
$myuser = $mybb->user['uid'];
$cookie_value = "$myuser ABCDE $sessionid";

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
        
    
}


The result creates a cookie called "my_session" with the following value:
0+ABCDE+2b9463c2ea380080735787d18af3c1db

I have been able to confirm that $sessionid is correct with each login, but instead of "1" (which is my UID), it keeps grabbing "0".  This is really bizarre because when I check the database, the session value is a match for my UID (1).  There are no sessions logged for UID "0" because you must be logged-in to view the page.


Any help would be appreciated!
Did you tried:
$cookie_value = $myuser."+ABCDE+".$sessionid;
(2015-09-24, 10:01 AM)SvePu Wrote: [ -> ]Did you tried:
$cookie_value = $myuser."+ABCDE+".$sessionid;

Yes.  With quotes, without quotes and with only $myuser.   $sessionid is correct, but $myuser is always set to 0.  For some reason the $myuser variable is not passing from MyBB to my plugin.  Am I using the wrong hook?
Try using $GLOBALS['user']['uid']
(2015-09-24, 08:53 PM)RateU Wrote: [ -> ]Try using $GLOBALS['user']['uid']

I tried this:
function myplugin_datahandler_login_complete_end()

{
global $mybb, $db, $session;
$GLOBALS['user']['uid'];

$cookie_name = "my_session";
$sessionid = $session->sid;
$myuser = $mybb->user['uid'];
$cookie_value = "$myuser ABCDE $sessionid";

setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
        
    
}


Unfortunately, I still do not get any value for UID.  Did I do something wrong?




I was able to circumvent the issue by adding this line of code to inc/datahandlers/login.php right after MyBB sets the "mybbuser" cookie.

my_setcookie("my_session", $user['uid'] . ";" . $session->sid, time() + 3600, "/");

I really did not want to do it that way, but until I can get the UID variable passed to my plugin, I do not know of any other solution.  It is strange that $SID is being passed, but not $UID. (Oh, and I tried using the "my_setcookie" function in my plugin just for kicks, but that did not manage to pull $UID either).
What I meant is replace $mybb->user['uid'] to $GLOBALS['user']['uid']
If you are hooking into datahandler_login_complete_end, the $mybb->user object is still stuck to a general guest data. The user data will be loaded afterwards, so you should grab the authenticated user data by using the argument passed in the hook, which is a "by-reference" hook type:

function myplugin_datahandler_login_complete_end($user)
{
  global $session;

  setcookie('my_session', $user['uid'] . ' ABCDE ' . $session->sid, time() + (86400 * 30), "/");    
}