1. 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

Discussion in 'XenForo Development Discussions' started by gginni, Jun 27, 2013.

  1. gginni

    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?
     
  2. AndyB

    AndyB Well-Known Member

    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;
    ?>
    
     
    Marcus likes this.
  3. gginni

    gginni Active Member


    Not working :(

    I need cookie info in different folder and not in forum folder... Will cookie be accessible outside the forum folder??
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    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.
     
    ForoStyle and LPH like this.
  5. AndyB

    AndyB Well-Known Member

    The thread title indicated external PHP file. I took that as meaning a stand alone file. Please ignore my previous post.
     
  6. gginni

    gginni Active Member

    @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
     
  7. gginni

    gginni Active Member

    Yeah external PHP file only.. Just folder structure is different..
     
  8. Chris D

    Chris D XenForo Developer Staff Member

    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. Dudey101

    Dudey101 New Member

    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. Clay Culver

    Clay Culver Member

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

    Dudey101 New Member

    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. JackBauer

    JackBauer Member

    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: Sep 28, 2014
  13. Robert9

    Robert9 Active Member

    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();
    }
     
  14. wang

    wang Well-Known Member

    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.
     
    otto likes this.
  15. Robert9

    Robert9 Active Member

    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: Mar 3, 2016
  16. otto

    otto Well-Known Member

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

Share This Page