XF 2.1 Continue an XF Session in PHP on the same domain?

⭐ Alex ⭐

Well-known member
Hey I have a section of my site that needs to get some user properties from the signed in XF user. (Using XF pages for this area is not an option.)

So far I'm using:

$app = XF::setupApp('XF\Pub\App');
$session = $app->session();
$user_id = $session->get('userId');
$finder = XF::finder('XF:User');
$user = $finder->where('user_id', $user_id)->fetchOne();

The problem is, when the user has been away for long enough, the session they get from this code is a visitor session, and the user has to go back to the forum so that it auto-signs them in, then when they come back to this external page, it finally sees them as logged in.

I found an old thread that used to have working code for XF 1, but I couldn't connect the XF 1 code to the XF 2 counter parts to determine how to accomplish it.

This is what I use, it works pretty well.
//Xenforo Session Setup
require ('path/to/xenforo' . '/src/XF.php');
$app = XF::setupApp('XF\Pub\App');
$user = XF::visitor();
then you can do things like:
Top Bottom