XF 2.0 I'm trying to get the user_id of the current user

I'm trying to get the user_id and permissions of the current logged in user for use in another application

I'm trying this... but all I get is 0.

Code:
$dir = __dir__;
require($dir . '/src/XF.php');
XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
$app->start();
$session = $app->session();
print_r($session->get('userId')) ;

I must be doing something wrong... any help appreciated.

Cheers

Blake
 
I'm trying to get the user_id and permissions of the current logged in user for use in another application

I'm trying this... but all I get is 0.

Code:
$dir = __dir__;
require($dir . '/src/XF.php');
XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
$app->start();
$session = $app->session();
print_r($session->get('userId')) ;

I must be doing something wrong... any help appreciated.

Cheers

Blake
The problem with your code (although you should be using LPH's code instead) is that "userId" with a capital i is not the correct index. On case-insensitive systems like Windows, this will work, but not on certain installations of macOS or on Linux-based servers where case-sensitivity is enabled.


Fillip
 
Thanks Guys,

Yes, I am logged in. This code appears in a file called api.php in the root of my XenForo Installation

Here's the code...

PHP:
<?php

$dir = __dir__;
require($dir . '/src/XF.php');
XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
$app->start();
$session = $app->session();

$visitor = \XF::visitor();
$user_id = $visitor['user_id'];
print_r($user_id) ;

I access this via https://url of my installatrion.com/api.php

I get 0

I must be missing something really simple.

Thanks again.
 
Have you tried to see if you are actually connecting? Try a dump($visitor) after visitor.

Instead of __DIR__, have you tried $_SERVER["DOCUMENT_ROOT"]?
 
Try to remove the $session

PHP:
$dir = __dir__;
require($dir . '/src/XF.php');
XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
$app->start();

$visitor = \XF::visitor();
$user_id = $visitor['user_id'];
print_r($user_id) ;
 
@LPH Thanks - Big fat 0 again

Is it due to the location of the script? Root folder?

Is there some sort of permission that is blocking this?

Is it because I'm a super admin?

I wont have much hair left if this keeps going on :)
 
This is what I use to connect:

PHP:
$fileDir =$_SERVER["DOCUMENT_ROOT"];

if ( ! file_exists( $fileDir . '/src/XF.php'  ) ) {
   return false;
}

require( $fileDir . '/src/XF.php' );
\XF::start($fileDir);

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

Next, I use this to grab user information:

PHP:
$visitor = \XF::visitor();
$user_id = $visitor['user_id'];
 
Thanks Guys,

Yes, I am logged in. This code appears in a file called api.php in the root of my XenForo Installation

Here's the code...

PHP:
<?php

$dir = __dir__;
require($dir . '/src/XF.php');
XF::start($dir);
$app = XF::setupApp('XF\Pub\App');
$app->start();
$session = $app->session();

$visitor = \XF::visitor();
$user_id = $visitor['user_id'];
print_r($user_id) ;

I access this via https://url of my installatrion.com/api.php

I get 0

I must be missing something really simple.

Thanks again.

Hi,
Maybe your root is some wrong, Please change this:
$fileDir = dirname(FILE) . '/../';
require $fileDir . '/src/XF.php';
XF::start($fileDir);
$app = XF::setupApp('XF\Pub\App');
$app->start();
$session = $app->session();
$visitor = \XF::visitor();
 
Top Bottom