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

Login / Logout On External Page

Discussion in 'XenForo Development Discussions' started by Davey-UK, Mar 24, 2015.

  1. Davey-UK

    Davey-UK Member

    Now searching the forums, a few threads have come up, but none seem to do the trick 100%.
    Do any of you Xenforo wizz's out there have a solution to show the login form on an external php page. (1)
    Once the user is logged in, show the welcome username etc and a logout link. (2)
    My page is on the same server, on the same domain, but outside the forums directory.

    It would be nice if the solution could be posted in this one forum, and not have to go piecing loads of answers together.
    I have looked at the api mod that has been posted on here, but its seems a bit of an overkill, for what to some of you wonderful people on here could post up to be short and sweet.

    Kind regards, and thanks for taking the time to help.
    Below is an image of what i mean.
    otto likes this.
  2. Davey-UK

    Davey-UK Member

    Right then, after alot of head scratching, i have pieced together quite a bit of code that is now working 100%.
    For anyone that may be interested, my code will display the following when inserted in an external page.
    1. Login form with a register button underneath if user is not logged in.
    2. Username, conversation count and alerts count and logout link if the user is logged in.
    3. X number of latest posts from your forum.
    4. X number of random images if you are using XenGallery.
    This code may not be aesthetically pleasing, but it does the job and hopefully will at least guide people in the right direction of putting Xenforo forum information into an external page.

    Firstly create a file named generalconfig.php with the following code and upload it into your sites root (where your external page is going to be. You will need to alter the site root and forum path to your own needs.
    // XenForo Authentication Loader
    $startTime = microtime(true);
    define('XF_ROOT', '/xx/xxx/xxx/your_domain.com/httpdocs/forums/'); // set this (absolute path)!
    define('TIMENOW', time());
    define('SESSION_BYPASS', true); // if true: logged in user info and sessions are not needed
    require_once(XF_ROOT . '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader(XF_ROOT . '/library');
    XenForo_Application::initialize(XF_ROOT . '/library', XF_ROOT);
    XenForo_Application::set('page_start_time', TIMENOW);
    $visitor = XenForo_Visitor::getInstance()->toArray();
    $userId = $visitor['user_id'];
    $username = $visitor['username'];
    $alerts = $visitor['alerts_unread'];
    $conversations = $visitor['conversations_unread'];
    Now include this file in your external file at the very first line using the following code.
    <?php include 'generalconfig.php';?>
    Next is to create a file to show the login form. Add the following code to a file called login1.php and upload it to your site root again. Dont forget to alter the domain name to your own.
    <div id="xenlogin">
       <form action="http://your_domain.com/forums/login/login" method="POST" id="pageLogin">
         <div class="ctrlWrapper">
             <font color="#000000"> <input type="text" name="login" id="LoginControl" class="textCtrl" tabindex="101"  placeholder="Username"/>
             <input type="password" name="password" id="ctrl_password" class="textCtrl" tabindex="102"  placeholder="Password"/>
           <input type="submit" class="button primary" value="log in" tabindex="104" data-loginPhrase="log_in" data-signupPhrase="sign_up" /></font>
            <!--<label for="ctrl_remember" class="rememberPassword">
             <input type="checkbox" name="remember" value="1" id="ctrl_remember" tabindex="103" /> Remember me</label>-->
         <input type="hidden" name="cookie_check" value="1" />
         <input type="hidden" name="redirect" value="/forums" />
         <input type="hidden" name="_xfToken" value=" ' . $XF->visitor->get( 'visitor.csrf_token_page' ) . ' " />
                            <a href="/forums/register" class="theme_button btn-sm"><i class="rt-icon-pen-alt-stroke"></i> Register</a>
    Next is to create a file to show the username, conversation and alert count with login link. Add the following code to a file called login2.php and upload it to your site root again.
    <form action="<?php echo XenForo_Application::get('options')->boardUrl; ?>/logout" method="post" name="myform">
    <!-- START USERNAME -->
    Hello <?php print_r($username); ?> &nbsp;&nbsp;
    <!-- END USERNAME -->
    echo '
    <span id="member" class="loggedIn_menu_class"><a href="/forums/conversations/">Inbox</a>  <strong class="itemCount">' . $conversations = $visitor['conversations_unread'] . ' </strong>
    <a href="/forums/account/alerts">Alerts</a> <strong class="itemCount">' . $alerts = $visitor['alerts_unread'] . ' </strong></span>
    <a href="javascript:document.myform.submit()">Logout</a>
    <input type="hidden" name="cookie_check" value="1" />
                    <input type="hidden" name="redirect"/forums" />
                    <input type="hidden" name="_xfToken" value="<?php echo $visitor['csrf_token_page']; ?>" />
                    <input type="hidden" name="_xfConfirm" value="1" />
    <!-- END LOGOUT LINK TEXT --> 
    Now to set the script to show the different files to logged in or not logged in users, you need to create another file named login4.php and again upload it to your site root.
    //require 'generalconfig.php';
    //$visitor = XenForo_Visitor::getInstance();
    if ($visitor['user_id'])
        include_once 'login2.php';
        include_once 'login1.php';
    Now include the previous file in the page where you wish to show the login/out functions with the following include code.
    <?php include 'login4.php';?>
    To make your alerts look a little better, you can add the following into your css file.
    .itemCount {
        background: none repeat scroll 0 0 #000;
        border-radius: 3px 3px 3px 3px;
        color: #CCCCCC;
        font-size: 11px;
        font-weight: bold;
        margin-left: 5px;
        padding: 1px 7px;
        text-align: center;
    Now go ahead and test your page.
    loggedin.jpg notloggedin.jpg
    Last edited: Apr 4, 2015
  3. Davey-UK

    Davey-UK Member

    Now to show x number of latest forum posts on an external page, create a file called latestposts.php containing the following code and again upload it to your site root.
    $forumUrl = '/forums/'; //Forum URL, must end with slash
    $limit = 5; // Display this number of last posts
    // Loading dependencies
    $xfDependencies = new XenForo_Dependencies_Public();
    // Initializing session
    // Reading viewable nodes list
    $nodeModel = XenForo_Model::create('XenForo_Model_Node');
    $viewableNodes = $nodeModel->getViewableNodeList();
    $nodeIds = array_keys($viewableNodes);
    $nodeIdsList = implode($nodeIds, ',');
    // Requesting last N viewable nodes
    $sql_forum = "SELECT `title`, `thread_id`, `view_count`, `reply_count`, `last_post_username`  FROM `xf_thread` WHERE `node_id` IN ($nodeIdsList) AND discussion_state='visible' ORDER BY `last_post_date` DESC LIMIT $limit";
    $lastThreads = XenForo_Application::get('db')->fetchAll($sql_forum);
    // Here you can insert the code you want
    // Displaying content
    foreach ($lastThreads as $thread) {
      // Constructing full path to threads
      $threadUrl = $threadUrl = $forumUrl. XenForo_Link::buildPublicLink('threads', $thread);
      // Trimming and escaping
      $threadTitle = XenForo_Template_Helper_Core::helperWordTrim($thread['title'], 30);
    $user = XenForo_Model::create('XenForo_Model_User')->getUserById(1);
      echo "<a href=\"$threadUrl\"><b>$threadTitle</b></a><br />Views: {$thread['view_count']}, Replies: {$thread['reply_count']}<br />";
      echo "By: {$thread['last_post_username']} <hr/>";
    Now include this in your external page using the following line of code.
     <?php include 'latestposts.php';?>
    You can alter the number of posts to show at the top of the file and remember that the external page must be .php and not .html
    Last edited: Apr 4, 2015
    Phantro and otto like this.
  4. Davey-UK

    Davey-UK Member

    If you have sonnb's XenGallery installed, you can pull images onto your external page with various formats to link to the original image using the following code and again uploading to your site root.
    Create a file name latestpictures.php. Change all the instances of "forums", to whatever your forums folder is called.
    $visitor = XenForo_Visitor::getInstance();
    $threadModel = XenForo_Model::create('XenForo_Model_Thread');
    if ($visitor->getUserId())
        // User array
        $userModel = XenForo_Model::create('XenForo_Model_User');
        $userinfo = $userModel->getFullUserById($visitor->getUserId());
                        $gallerymodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_Album');
                        $contentmodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_Content');
                        $photomodel = sonnb_XenGallery_Model_Abstract::create('sonnb_XenGallery_Model_ContentData');
    // The number 12 below is how many users albums you would like to pull images from
               $albums = $gallerymodel->getAlbums(array('photo_count' => array('>=',1)), array('order' => 'random', 'limit' => 12, 'offset' => 0));
                        foreach ($albums as $album) {
                                $albumurl = preg_replace('/[^a-zA-Z0-9\-\s]+/','',strtolower($album['title']));
                                $albumurl = str_replace(' ','-',$albumurl);
                              // $photo = array();
    // The number 1 below is the number of images from the amount of users albums above to show. eg 12 users albums with one from each will show 12 images in total.
    $fetchOptions =
    array('order' => 'content_date', 'orderDirection' => 'desc', 'limit' => 1, 'offset' => 0, 'join' =>
        sonnb_XenGallery_Model_Content::FETCH_DATA | sonnb_XenGallery_Model_Content::FETCH_PHOTO
    $contents = $contentmodel->getContentsByAlbumId($album['album_id'],  array(), $fetchOptions);
    $contents= $contentmodel->prepareContents($contents, $fetchOptions);
    foreach ($contents as $photo) {
        $photourl = preg_replace('/[^a-zA-Z0-9\-\s]+/','',strtolower($photo['title']));
        $photourl = str_replace(' ','-',$photourl);
    echo '<a href="/forums/gallery/photos/'.$photourl.'.'.$photo['content_id'].'/" title="View photo '.$photo['title'].'"><img src="/forums/' . $photo['thumbnailUrl'] . '" title="Image by: ' . $photo['username'] . '" style="border: solid 1px #000;" hspace="3" vspace="3" height="65" width="65"/></a>';
    //echo '<a href="' . XenForo_Link::buildPublicLink('/forums/gallery/photos', $photo) . '" title="View photo '.$photo['title'].'"><img src="/forums/' . $photo['thumbnailUrl'] . '" title="Image by: ' . $photo['username'] . '" style="border: solid 1px #000;" hspace="3" vspace="3" height="65" width="65"/></a>';
    Now include it in your external page using the following include code.
    <?php include 'latestpictures.php';?>
    Hopefully this will help out many people that have been searching for many weeks for such solutions.
    Dan Allen and otto like this.
  5. LPH

    LPH Well-Known Member

    I like what you've done but there are a few ways to tighten up the latest thread code. For example, you don't need the forumURL and you have repeated equal in the line:

    $threadUrl $threadUrl $forumUrlXenForo_Link::buildPublicLink('threads'$thread);
    Instead use:
    $threadUrl =XenForo_Link::buildPublicLink('canonical:threads'$thread);
    Dan Allen likes this.
  6. Phantro

    Phantro New Member

    @Davey-UK, I just have no words!

    I'm almost leaving from XenForo. 3 days trying to use these type of rest api...
    I tried 2 REST API, a LOT of logins plugins, and I'm almost 3 days trying do do that and ALL I NEED is here, just in 2 posts.


    I'm tired, here now is almost 6 am, 3 days almost without sleep just trying to do EXACTLY THE SAME YOU POST HERE.
    Coming from SMF and SMF has the WONDERFUL ssi.php script of that no one paid software have.

    I just need... external login and logout, read some user data (like username usergroup), and latest posts and this is ALL HERE.

    Try edit post and insert in tags (ssi.php, smf ssi) I'm sure this will help a lot of peoples!

    Again! thank you for all!
    Davey-UK and StarArmy like this.

Share This Page