User Sessions Not Working - External PHP File


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.


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.


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.

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.


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.


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!