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.
- Login form with a register button underneath if user is not logged in.
- Username, conversation count and alerts count and logout link if the user is logged in.
- X number of latest posts from your forum.
- 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.
Code:
<?php
// 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);
XenForo_Application::disablePhpErrorHandler();
XenForo_Application::setDebugMode(false);
XenForo_Session::startPublicSession();
$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.
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.
Code:
<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>-->
</div>
<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' ) . ' " />
</form>
</div>
<span>
<a href="/forums/register" class="theme_button btn-sm"><i class="rt-icon-pen-alt-stroke"></i> Register</a>
</span>
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.
Code:
<form action="<?php echo XenForo_Application::get('options')->boardUrl; ?>/logout" method="post" name="myform">
<!-- START USERNAME -->
Hello <?php print_r($username); ?>
<!-- END USERNAME -->
<!-- START ALERTS AND INBOX COUNT -->
<?php
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>
';
?>
<!-- END ALERTS AND INBOX COUNT -->
<!-- START LOGOUT LINK TEXT THAT WILL NOT NEED CONFIRMATION -->
<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" />
</form>
<!-- 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.
Code:
<?php
//require 'generalconfig.php';
//$visitor = XenForo_Visitor::getInstance();
if ($visitor['user_id'])
{
include_once 'login2.php';
}
else
{
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.
Code:
<?php include 'login4.php';?>
To make your alerts look a little better, you can add the following into your css file.
Code:
.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.