1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Logged in User's ID in Memcache

Discussion in 'XenForo Development Discussions' started by CometChat Team, Dec 15, 2015.

  1. Hello,

    I have enabled Memcached as a backend for caching system. I am developing a plugin in which I need to get the ID of the logged in user. Can someone help me out to find as to how can I retrieve the ID when memcached is enabled.
    P.S the following code snippet that I had used which did not work:

    if($userid == 0){
    $fileDir = dirname(dirname(__FILE__));
    require($fileDir . '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
    XenForo_Application::initialize($fileDir . '/library', $fileDir);
    XenForo_Session::startPublicSession();
    $visitor = XenForo_Visitor::getInstance()->toArray();
    $userid = $visitor['user_id'];
    }
    This code helped me get the userid, but throws a Fatal error while re-installing my plugin.
    Please help.
     
  2. Robust

    Robust Well-Known Member

    Shouldn't need to change anything and toArray isn't needed. What's the fatal error you get?
     
  3. Thanks @Robust for replying.


    Our plugin loads the following code on all pages administration as well as frontend.

    if($userid == 0){
    $fileDir = dirname(dirname(__FILE__));
    require($fileDir . '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');
    XenForo_Application::initialize($fileDir . '/library', $fileDir);
    XenForo_Session::startPublicSession();
    $visitor = XenForo_Visitor::getInstance()->toArray();
    $userid = $visitor['user_id'];
    }


    On the front end this works without a hitch as XenForo_Application is not initialized.
    However, XenForo_Application is already initialized in the administration panel when admin is loaded. Hence when our plugin is loaded the following statement throws a fatal error as the initialize function does not check if XenForo_Application is initialized or not:
    XenForo_Application::initialize($fileDir . '/library', $fileDir);
    We don't know how to find out if XenForo_Application has been initialized or not and need help with the same.
     
  4. Robust

    Robust Well-Known Member

    I'd need to know the context in which you do this. What are you trying to do?
     
  5. We are trying to fetch the userid of logged in Xenforo user in a php file, which is inside our folder placed in the root folder of Xenforo. We send AJAX calls to this file periodically to fetch logged in users details. This file is also executed (included) whenever any of our plugin's page is loaded. We use free floating core PHP functions (no OOPS). On examining Xenforo files we found the above code in the Xenforo admin panel files which works well on the frontend but when executed on the backend causes fatal error due to above mentioned issue.
     
  6. rellect

    rellect Well-Known Member

    Not ideal but should work
    PHP:
    if($userid == 0){
        
    $fileDir dirname(dirname(__FILE__));

        if(!
    class_exists('XenForo_Autoloader'))
        {
            require(
    $fileDir '/library/XenForo/Autoloader.php');
            
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir '/library');
        }

        try{
            
    XenForo_Application::initialize($fileDir '/library'$fileDir);
            
    XenForo_Session::startPublicSession();
        }
        catch(
    Exception $e){}

        
    $visitor XenForo_Visitor::getInstance()->toArray();
        
    $userid $visitor['user_id'];
    }
     
  7. Robust

    Robust Well-Known Member

    Sounds to me like you should be using the Misc controller for this and adding an action, a custom controller or injecting into one.
     

Share This Page