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

Get user information (custom field) in Kotomi script

Discussion in 'XenForo Development Discussions' started by Mathis Neumann, Aug 13, 2012.

  1. Hi there,
    I'm using Kotomi (http://xenforo.com/community/resources/kotomi-generic-script-bridge.122/) to implement a php script I coded before I got to know xenforo. I don't know anything about how to program an addon and its not what I want to.

    I just need a "simple" way to get some information about the user within my own script, especially the online status, username and a custom field. Is there a way to get this to work?

    Thanks
     
  2. Jake Bunce

    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
    }
    
     
    AndreaDB and nothingatall like this.
  3. Thanks! Works perfectly! :)
     
  4. Zephyr

    Zephyr Well-Known Member

    Hello, what is it please ?
    ob_start();
     
  5. Russ

    Russ Well-Known Member


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

    Would it be the KotomiHeader.php?
     
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    In the example-index.php file from the kotomi zip file, in the area where the output is (the echo statement).
     
  7. Russ

    Russ Well-Known Member

    Sorry to bug you would you know if these conditionals would still work if I used an external file?
     
  8. Jake Bunce

    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.
     
  9. Russ

    Russ Well-Known Member

    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
     
  10. Jake Bunce

    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 likes this.
  11. Russ

    Russ Well-Known Member

    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
     
  12. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Custom fields should be:

    Code:
    echo $visitor['customFields']['field_id'];
    
    Group checking:

    Code:
    if ($visitor->isMemberOf(array(3,4,5)))
    {
    
    }
    
     
    Russ likes this.
  13. Russ

    Russ Well-Known Member

    To further this Jake sorry if this is a dumb question, how do you go about outputting the username, plain no links.
     
  14. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Code:
    echo $visitor['username'];
    
     
    LPH and Russ like this.
  15. AndreaDB

    AndreaDB Member

    Hi Jake,
    how can I handling my output in Kotomi php with Templates?
    Thanks! :)
     
  16. AndreaDB

    AndreaDB Member

    @Jake Bunce can you please explain me how can I handling my output using template in Kotomi php?
    Thanks :)
     

Share This Page