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

User Sessions Not Working - External PHP File

Discussion in 'XenForo Development Discussions' started by WSWD, Jun 20, 2013.

  1. WSWD

    WSWD Well-Known Member

    I'm sure there is a very simple fix, but after hours of coding, I'm just not seeing it. :sleep:

    Using the following code to get the user info:

    define('XF_ROOT', '/home/hydro/public_html/forum/'); // set this!
    define('TIMENOW', time());
    define('SESSION_BYPASS', false); // if true: logged in user info and sessions are not needed
    require_once(XF_ROOT . '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader(XF_ROOT . '/library');
    XenForo_Application::initialize(XF_ROOT . '/library', XF_ROOT);
    XenForo_Application::set('page_start_time', TIMENOW);
        $visitor = XenForo_Visitor::getInstance();
        if ($visitor->getUserId())
            $userModel = XenForo_Model::create('XenForo_Model_User');
            $userinfo = $userModel->getFullUserById($visitor->getUserId());

    That works great when I use this in a file in the root directory (i.e. public_html/). As soon as I move the file to a subdirectory (i.e. public_html/directory), it no longer works.

    This doesn't make any sense as there are no relative paths in the code...only absolute paths. Any help would be greatly appreciated.

  2. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Where does it fail exactly? Is there an error? What is happening?

    In your usage there would normally be no ending slash on XF_ROOT. You might try that.
    WSWD likes this.
  3. WSWD

    WSWD Well-Known Member

    Thanks for the quick reply Jake.

    I tried with and without the trailing slash earlier and it didn't matter unfortunately.

    There are no errors, there is just no value being returned. I'm using echo $userinfo['username']; to return the username, for example. Returns the username just fine when I have the file in the root directory, but as soon as I put it into a subdomain, nothing.
  4. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    But where exactly is it failing? On session creation? On fetching the user record? You need to debug it.

    I don't see any obvious problem off hand.
  5. WSWD

    WSWD Well-Known Member

    Honestly, I don't know. Any quick way to figure that out?

    I just don't understand why it would matter where I put the file on the server. I don't see anything that would make a difference.
  6. WSWD

    WSWD Well-Known Member

    In case it helps anyone (and let me stress...this makes absolutely NO sense), I had a php.ini file in the directory to limit upload size. Removed that file and everything worked fine. Put the file back and everything worked fine. Literally nothing else changed.

    Who knows? ha ha!
  7. WSWD

    WSWD Well-Known Member

    Nope...back to not working. Hmmmmmm... No clue

Share This Page