XF 2.0 How to check login status outside xenforo

ZecaR

New member
I'm here after 2 hours of searching. All I found were older versions solutions

here is my code so far

PHP:
<?php
    
$dir = __DIR__;
require($dir . '/src/XF.php');

XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
echo "<pre>";
var_dump($app->session());

?>

The session contains the userId, but it's protected. Don't know what to do next. Tried searching through xf code but it's too much
 
If you want to access the data from things like the session and what it sets up, you need to call the start() method on the app after setting it up. The visitor should then be setup for you.
 
If you want to access the data from things like the session and what it sets up, you need to call the start() method on the app after setting it up. The visitor should then be setup for you.
ok, the XF::visitor() is set now that I called $app->start(). But all the properties are protected. What do I do now ?
 
Can you please share your code as I am stuck, too.

All I want is to get the username of the xenforo user who is accessing my php script. Any examples or ideas? Thank you! :)
 
Last edited:
Can you please share your code as I am stuck, too.

All I want is to get the username of the xenforo user who is accessing my php script. Any examples or ideas? Thank you! :)
Late to the party, but Google brought me here. This is what you need. Demonstrating starting the XF app, getting the visitor object, checking user_id, secondary groups, and username

PHP:
<?php

#    print_r($_COOKIE);

    $fileDir = '../';  # relative path from this script to the Xenforo root
    require($fileDir . '/src/XF.php');
    XF::start($fileDir);
    $app = XF::setupApp('XF\Pub\App');
    $app->start();
    print_r (XF::visitor());  # dumps entire object
    print("<br>");
    $user=XF::visitor();
    print ($user->user_id."<br>"); # = 1 (0 if nobody logged in)

    if (in_array(18,$user->secondary_group_ids))
        print ("Group 18<br>");

    print($user->username."<br>"); # = "Mick West"

    exit(0);
 
Top Bottom