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

Pawn Studios

Active 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:
PHP:
require('forum/src/XF.php');

XF::start("/");
$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.

 

Gomez

Member
This is what I use, it works pretty well.
PHP:
//Xenforo Session Setup
require ('path/to/xenforo' . '/src/XF.php');
XF::start(__DIR__);
$app = XF::setupApp('XF\Pub\App');
$app->start();
$user = XF::visitor();
then you can do things like:
PHP:
$user['user_id']
$user['username']
 
Top