XF 2.0 How can I get username of logged user on PHP?

TufanM

New member
I'm trying to build an external shop for my website, i need to get logged user's username on my PHP application but i have no idea how.

How can i access to user data from PHP?
 
Code:
require ('/src/XF.php');

XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
$session = $app->session();

That should be able to get the XenForo Session.

To access the visitor data, you can get that by
Code:
$visitor = \XF::visitor();
 
Code:
require ('/src/XF.php');

XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
$session = $app->session();

That should be able to get the XenForo Session.

To access the visitor data, you can get that by
Code:
$visitor = \XF::visitor();

I am using the code below, it works. Is this the right implementation? Can someone fake their user ids by editing cookies etc.?
PHP:
require('src/XF.php');
\XF::start('/hc');
$app = \XF::setupApp('XF\Pub\App');
$s = $app->session();
$uid = $s->get('userId');
if ($uid){
    $finder = \XF::finder('XF:User');
    $user = $finder->where('user_id', $uid)->fetchOne();
    print_r($user);
}else{
    echo 'guest';
}
 
These are sessions, not cookies. Sessions are hashed and set from the server. No one would be able to change it, unless they change the entire hash.
 
Back
Top Bottom