Get user information (custom field) in Kotomi script

Jake Bunce

XenForo moderator
Staff member
It's funny you should ask this. I just dealt with this problem for a kotomi-based script I made for some one.

Within the kotomi script you will have access to all XenForo classes including the visitor object. But the kotomi script doesn't setup the session like a normal XenForo page because it doesn't use a controller. So you need to start the session before accessing the visitor object. Like this:

Code:
XenForo_Session::startPublicSession(new Zend_Controller_Request_Http);

$visitor = XenForo_Visitor::getInstance();
Now $visitor contains all information for the current logged in user, including custom fields. For example:

Code:
if ($visitor['user_id'])
{
	// IS LOGGED IN
}
else
{
	// IS NOT
}
 

Russ

Well-known member
It's funny you should ask this. I just dealt with this problem for a kotomi-based script I made for some one.

Within the kotomi script you will have access to all XenForo classes including the visitor object. But the kotomi script doesn't setup the session like a normal XenForo page because it doesn't use a controller. So you need to start the session before accessing the visitor object. Like this:

Code:
XenForo_Session::startPublicSession(new Zend_Controller_Request_Http);
 
$visitor = XenForo_Visitor::getInstance();
Now $visitor contains all information for the current logged in user, including custom fields. For example:

Code:
if ($visitor['user_id'])
{
// IS LOGGED IN
}
else
{
// IS NOT
}

Jake do you recall where exactly you put that first snippet of code?

Would it be the KotomiHeader.php?
 

Jake Bunce

XenForo moderator
Staff member
Jake do you recall where exactly you put that first snippet of code?

Would it be the KotomiHeader.php?
In the example-index.php file from the kotomi zip file, in the area where the output is (the echo statement).
 

Jake Bunce

XenForo moderator
Staff member
My kotomi index looks like this:

Code:
<?php
  
$startTime = microtime(true);
$kotomi_indexFile = "./";
$kotomi_container = true;
$fileDir = dirname(__FILE__)."/{$kotomi_indexFile}";
require "{$fileDir}/library/Dark/Kotomi/KotomiHeader.php";

include('./external.php');

require "{$fileDir}/library/Dark/Kotomi/KotomiFooter.php";
Then I do the session stuff inside of external.php. It's still the same place in the execution and the same scope, whether it's inline or part of an external file.
 

Russ

Well-known member
My kotomi index looks like this:

Code:
<?php
 
$startTime = microtime(true);
$kotomi_indexFile = "./";
$kotomi_container = true;
$fileDir = dirname(__FILE__)."/{$kotomi_indexFile}";
require "{$fileDir}/library/Dark/Kotomi/KotomiHeader.php";
 
include('./external.php');
 
require "{$fileDir}/library/Dark/Kotomi/KotomiFooter.php";
Then I do the session stuff inside of external.php. It's still the same place in the execution and the same scope, whether it's inline or part of an external file.
I got that part down I guess the part I'm missing is the actual session stuff.

I normally try not to ask many questions just answer them the best I can ;p

How would I go about doing something like this:

Code:
<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
Show to guest

Show to admins
 
</body>
</html>
Assuming that code is in the external.php
 

Jake Bunce

XenForo moderator
Staff member
How are you handling your output? Templates? Echo statements? Use some kind of condition statement that checks the visitor object.

In raw PHP using echo statements:

Code:
XenForo_Session::startPublicSession(new Zend_Controller_Request_Http);

$visitor = XenForo_Visitor::getInstance();

if ($visitor['user_id'])
{
	echo 'IS LOGGED IN';
}
else
{
	echo 'IS NOT';
}
 

Russ

Well-known member
How are you handling your output? Templates? Echo statements? Use some kind of condition statement that checks the visitor object.

In raw PHP using echo statements:

Code:
XenForo_Session::startPublicSession(new Zend_Controller_Request_Http);
 
$visitor = XenForo_Visitor::getInstance();
 
if ($visitor['user_id'])
{
echo 'IS LOGGED IN';
}
else
{
echo 'IS NOT';
}
Perfect Jake, got it down, lastly you said this works with custom profile fields, am I able to output a custom user field text on this page through a statement like that?

Lastly...

how can I work in if ($visitor['user_id']) to work with group #'s if possible, if member is group of x y z ect
 

Jake Bunce

XenForo moderator
Staff member
Custom fields should be:

Rich (BB code):
echo $visitor['customFields']['field_id'];
Group checking:

Rich (BB code):
if ($visitor->isMemberOf(array(3,4,5)))
{

}
 

Russ

Well-known member
Custom fields should be:

Rich (BB code):
echo $visitor['customFields']['field_id'];
Group checking:

Rich (BB code):
if ($visitor->isMemberOf(array(3,4,5)))
{
 
}
To further this Jake sorry if this is a dumb question, how do you go about outputting the username, plain no links.
 
Top