How to get user cookie or user name /user id in external PHP file

gginni

Active member
Hi,

I have a xenforo forum installation at www.domainname.com/forum (under root/forum folder).

I want to know logged-in user (user-name or user-id) in www.domainname.com/index.php (Means root/index.php)

How to achieve this?

BTW index.php at root is just home page where I want to show Hello Username to my logged-in user. I guess it's possible but please tell me how?
 
You can obtain the userid from the xf_user cookie.

Code:
<?php
$xf_user = $_COOKIE["xf_user"];
$pos = strpos($xf_user, ',');
$userid = substr($xf_user,0,$pos);
echo $userid;
?>
 
You can obtain the userid from the xf_user cookie.

Code:
<?php
$xf_user = $_COOKIE["xf_user"];
$pos = strpos($xf_user, ',');
$userid = substr($xf_user,0,$pos);
echo $userid;
?>


Not working :(

I need cookie info in different folder and not in forum folder... Will cookie be accessible outside the forum folder??
 
This is just an alternative to grabbing it via the cookie (there's nothing wrong with doing that, this is just an alternative).

This effectively loads XenForo from an external script. You will have to adjust the $fileDir variable to your environment (I am currently at a Windows PC, which will explain the dodgy path :p):

PHP:
<?php

$startTime = microtime(true);
$fileDir = 'C:\Program Files (x86)\Zend\Apache2\htdocs';

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

XenForo_Session::startPublicSession();

$visitor = XenForo_Visitor::getInstance()->toArray();

You now have the entire visitor record to you as an array.

So:

PHP:
$userId = $visitor['user_id'];
$username = $visitor['username'];

You can do a heck of a lot more with that small bit of code as well, but for now that does what you asked.
 
The thread title indicated external PHP file. I took that as meaning a stand alone file. Please ignore my previous post.
 
@Chris Deeming - I understood what u are trying to say but dirty way :p... Is there any easy way to handle such kinda scenario??

Neat and clean way

That is a neat and clean way. It's a nice way of doing it because you're properly initialising XenForo and accessing the data in the same way as you would access it in the core XenForo code. My method directly accesses the XF API.
 
Been trying to get something to work that will enable me to check that a forum member is logged in and display their user id. That's all I wanted to do as a test.

However, I simply cannot get any of this to work and I've tried all the different methods mentioned in this thread (and others).

I have used the example given above as a template and this is what I've got:-

PHP:
<?php

$startTime = microtime(true);
$fileDir = '/home/MYDOMAIN/public_html/forum';

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

XenForo_Session::startPublicSession();

$visitor = XenForo_Visitor::getInstance()->toArray();

$userId = $visitor['user_id'];
$username = $visitor['username'];
echo "User id = ".$userId;
?>

This script is not placed in my forum directory (which, as you can see is a sub-directory called "forum") but is run from my root domain i.e. www.mydomain.com/test.php

However, all I get is a zero for user id.

One script I tried to run mentioned zend when I tried to run it - could this be the problem? I know that another piece of software wouldn't run properly without the Zend application being re-configured (I don't know what was done, though - the vendor sorted it for me).
 
The easiest way to get this to work for you is to put the code that @Chris D was nice enough to post in to a file called xf_connector.php then in index.php do the following

Code:
<?php
require 'xf_connector.php';
?>
<!DOCTYPE html>
<html>
    <head>
<title>My first page</title>
<meta charset="UTF-8">
    </head>
<body>
<p>Hello <?php print_r($username); ?> welcome to this web page.</p>
</body>
</html>
 
Thanks all in here - especially Chris D - I've managed to get the forum user id onto an external php page. The world is now my lobster... or something...
 
You can obtain the userid from the xf_user cookie.

Code:
<?php
$xf_user = $_COOKIE["xf_user"];
$pos = strpos($xf_user, ',');
$userid = substr($xf_user,0,$pos);
echo $userid;
?>

I don't have an xf_user cookie, only xf_session. Is this still a feasible way of obtaining user information?

Edit: nevermind. Apparently the xf_user cookie is only created when "remember me" is selected.
 
Last edited:
I am really happy to found this. Now i can work like with vb. Is there any list of possible vars, please?
I need something like

if (!($permissions['forumpermissions'] & $vbulletin->bf_ugp_forumpermissions['canpostnew']))
{
print_no_permission();
}
 
I am really happy to found this. Now i can work like with vb. Is there any list of possible vars, please?
I need something like

if (!($permissions['forumpermissions'] & $vbulletin->bf_ugp_forumpermissions['canpostnew']))
{
print_no_permission();
}

Assuming that you have initialized xenforo 's framework in your external script, you can use $visitor->hasPermission() function to check for permissions, and pass the permission group and permission name as paramaters.

e.g:

PHP:
$visitor = XenForo_Visitor::getInstance();
if($visitor->hasPermission('group_name', 'permission_name'))
{
   echo 'You are not allwed here';
}

This is to use your own custom permissions. If you want to use that of xenforo, then replace the group and permission name with whatever permissions you want to use.
 
Thank you, i just use postReply as the permission to vote or not at the external page.

I tried this:
Code:
$startTime = microtime(true);
$fileDir = '/var/www/...';

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

XenForo_Session::startPublicSession();

$visitor = XenForo_Visitor::getInstance()->toArray();

$userid = $visitor['user_id'];
$username = $visitor['username'];
$allowed= $visitor[permissions][forum][postReply]; // 1 = allowed

While i am logged in everything is nice, but if not i got a white page with something like: "Something strange is happened, come later again"

Any idea how i can manage this, please?
Normally i just want to say: no userid, pass a text: Please login and show the page.
 
Last edited:
Please take a look at your error logs (Server/Webhosting) at the time you try it out and post them here. Thanks. :)
 
This is just an alternative to grabbing it via the cookie (there's nothing wrong with doing that, this is just an alternative).

This effectively loads XenForo from an external script. You will have to adjust the $fileDir variable to your environment (I am currently at a Windows PC, which will explain the dodgy path :p):

PHP:
<?php

$startTime = microtime(true);
$fileDir = 'C:\Program Files (x86)\Zend\Apache2\htdocs';

require($fileDir . '/library/XenForo/Autoloader.php');
XenForo_Autoloader::getInstance()->setupAutoloader($fileDir . '/library');

XenForo_Application::initialize($fileDir . '/library', $fileDir);
XenForo_Application::set('page_start_time', $startTime);

XenForo_Session::startPublicSession();

$visitor = XenForo_Visitor::getInstance()->toArray();

You now have the entire visitor record to you as an array.

So:

PHP:
$userId = $visitor['user_id'];
$username = $visitor['username'];

You can do a heck of a lot more with that small bit of code as well, but for now that does what you asked.


@Chris D can we get a XF 2.x version of this code?

thanks
arn
 
You generally need fewer lines of code to do it in XF 2.x.

PHP:
<?php

$dir = __DIR__;
require ($dir . '/src/XF.php');

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

$dir needs to point to the root of your XF installation and if it does then this PHP file can be in any directory.

And then you just add whatever additional code you need, e.g.:

PHP:
$visitor = \XF::visitor();
$userId = $visitor->user_id;
$username = $visitor->username;

Once you've got the $app object you've basically got access to the entire framework.
 
  • Like
Reactions: arn
You generally need fewer lines of code to do it in XF 2.x.

PHP:
<?php

$dir = __DIR__;
require ($dir . '/src/XF.php');

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

$dir needs to point to the root of your XF installation and if it does then this PHP file can be in any directory.

And then you just add whatever additional code you need, e.g.:

PHP:
$visitor = \XF::visitor();
$userId = $visitor->user_id;
$username = $visitor->username;

Once you've got the $app object you've basically got access to the entire framework.
Quick question - are there any additional lines of code needed with this? I'm working with a local installation and it keeps pulling 0 for a user_id and obviously an empty username. I'm assuming it's something session-related, so I'm wondering if it's just because it's a local xampp install or if I'm potentially missing another line that's not grabbing the cookie? I'm logged in on the forum and the admin panel and the local board installation is running without an issue, but it doesn't appear to be working on my external test.php file...?
 
PHP:
<?php

$dir = __DIR__;
require ($dir . '/src/XF.php');

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

Missed a line. That should work.
 
Top Bottom