• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

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

AndyB

Well-known member
#2
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;
?>
 

gginni

Active member
#3
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??
 

Chris D

XenForo developer
Staff member
#4
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.
 

AndyB

Well-known member
#5
The thread title indicated external PHP file. I took that as meaning a stand alone file. Please ignore my previous post.
 

Chris D

XenForo developer
Staff member
#8
@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.
 
#9
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).
 
#10
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>
 
#11
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...
 
#12
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:

Robert9

Active member
#13
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();
}
 

wang

Well-known member
#14
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.
 

Robert9

Active member
#15
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:

otto

Well-known member
#16
Please take a look at your error logs (Server/Webhosting) at the time you try it out and post them here. Thanks. :)