• This forum has been archived. New threads and replies may not be made. All add-ons/resources that are active should be migrated to the Resource Manager. See this thread for more information.

Grab session info in an external PHP file

Benjy

Well-known member
Here's the code allowing to include the xenForo framework inside an "external" PHP file (eg. if you don't want to use the template system... but cookies must be accessible), thus accessing visitor (and other) data. Comments are welcome.
PHP:
$startTime = microtime(true);
$fileDir = dirname(__FILE__) . '/xf'; //my xenForo install is located in webroot/xf/ and my script is in webroot/

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);

class FooBar extends XenForo_Controller
{
    public function responseNoPermission()
    {
        return $this->responseError(new XenForo_Phrase('do_not_have_permission'), 403);
    }
}

$controller = new FooBar(new Zend_Controller_Request_Http(), new Zend_Controller_Response_Http(), new XenForo_RouteMatch());
$controller->preDispatch('');

I'm not sure if extending the XenForo_Controller class is the best way to go, or if we should intialize XenForo_Session "by hand". Anyways, once these lines are executed, you can simply instantiate the visitor class with a simple
PHP:
$visitor = XenForo_Visitor::getInstance();
 
Ah, wrong forum... could a moderator move this to "Development Tutorials and Resources" please?
 
Top Bottom