External login/logout functions

fronix

Member
Hello,

I've searched the forums here and found some really helpful posts but they missing some stuff that I need explained.

This is my current login function but I'm not sure of how it works. Earlier today this worked but now I have done something and now it wont work and can't figure out the problem.

PHP:
    function login($data)
    {
        $loginModel = XenForo_Model::create('XenForo_Model_Login');
        $userModel = XenForo_Model::create('XenForo_Model_User');
        $error = "";
 
        $userid = $userModel->validateAuthentication($data['login'], $data['password'], $error);
        if (!$userid)
        {
            $loginModel->logLoginAttempt($data['login']);
            return $error;
        }
 
        $loginModel->clearLoginAttempts($data['login']);
 
        if ($data['remember'])
        {
            $userModel->setUserRememberCookie($userid);
        }
 
        XenForo_Model_Ip::log($userid, 'user', $userid, 'login');
 
        $userModel->deleteSessionActivity(0, $this->request->getClientIp(false));
 
        $session = XenForo_Application::get('session');
 
        $session->changeUserId($userid);
        XenForo_Visitor::setup($userid);
 
        return;
    }

I keep getting this error
Code:
An exception occurred: No entry is registered for key 'session' in /storage/content/82/10398/public_html/forum/library/XenForo/Application.php on line 815
 
    XenForo_Application::get() in /storage/content/82/10398/public_html/includes/functions.php at line 49
    Session->login() in /storage/content/82/10398/public_html/index.php at line 16


And this is what I include on the index.php page
PHP:
require_once('forum/library/XenForo/Autoloader.php');
$startTime = microtime(true);
 
XenForo_Autoloader::getInstance()->setupAutoloader('forum/library');
 
XenForo_Application::initialize('forum/library', XF_ROOT);
XenForo_Application::set('page_start_time', $startTime);
XenForo_Application::disablePhpErrorHandler();
XenForo_Application::setDebugMode(true);
include("includes/functions.php");
 
$isOnline = $session->isOnline();
$avatarUrl = XenForo_Template_Helper_Core::callHelper('avatar', array($isOnline, 'm', null, false));
 
restore_error_handler();
restore_exception_handler();

And this is my functions file
PHP:
<?php
 
class Session {
   
    #Xenforo if logged in function
    function isOnline(){
    $session = new XenForo_Session();
        if (!$session->sessionExists())
            {//when user info and sessions are needed
                XenForo_Session::startPublicSession();
 
                $visitor = XenForo_Visitor::getInstance();
 
                if ($visitor->getUserId())
                {
                    $userModel = XenForo_Model::create('XenForo_Model_User');
                    $userinfo = $userModel->getFullUserById($visitor->getUserId());
                        return $userinfo;
                }
            }else{
            return false;
            }
    }
   
    function login($data)
    {
        $loginModel = XenForo_Model::create('XenForo_Model_Login');
        $userModel = XenForo_Model::create('XenForo_Model_User');
        $error = "";
 
        $userid = $userModel->validateAuthentication($data['login'], $data['password'], $error);
        if (!$userid)
        {
            $loginModel->logLoginAttempt($data['login']);
            return $error;
        }
 
        $loginModel->clearLoginAttempts($data['login']);
 
        if ($data['remember'])
        {
            $userModel->setUserRememberCookie($userid);
        }
 
        XenForo_Model_Ip::log($userid, 'user', $userid, 'login');
 
        $userModel->deleteSessionActivity(0, $this->request->getClientIp(false));
 
        $session = XenForo_Application::get('session');
 
        $session->changeUserId($userid);
        XenForo_Visitor::setup($userid);
 
        return;
    }
   
    function logout()
        {
            // remove an admin session if we're logged in as the same person
            if (XenForo_Visitor::getInstance()->get('is_admin'))
            {
                $adminSession = new XenForo_Session(array('admin' => true));
                $adminSession->start();
                if ($adminSession->get('user_id') == XenForo_Visitor::getUserId())
                {
                    $adminSession->delete();
                }
            }
           
            XenForo_Model::create('XenForo_Model_Session')->processLastActivityUpdateForLogOut(XenForo_Visitor::getUserId());
 
            XenForo_Application::get('session')->delete();
            XenForo_Helper_Cookie::deleteAllCookies(
                array('session'),
                array('user' => array('httpOnly' => false))
            );
 
            XenForo_Visitor::setup(0);
 
            return true;
        }
}
 
$session = new Session;
?>
 
Hi fronix!

I copied/pasted your code and I don't have any error. Just be careful because XF_ROOT is undefined and new Session() requires parenthesises. I would also put the restore error/exception stuff right after your include. Other than that, I'm not able to reproduce your problem.
 
Hi fronix!

I copied/pasted your code and I don't have any error. Just be careful because XF_ROOT is undefined and new Session() requires parenthesises. I would also put the restore error/exception stuff right after your include. Other than that, I'm not able to reproduce your problem.

Thanks for the reply!

XF_ROOT is not undefined I do have a global config file that I store all the constants in. Though could you explain what you mean with parenthesises, and how does the restore/exception "stuff" look like ^^
 
When I say "requires", it's more good habits than anything :)
PHP:
$session = new Session;

This should go before your include functions:
PHP:
restore_error_handler();
restore_exception_handler();

Ah ok, you wouldn't know how to make a function that can read all forum posts from a specific forum into an array so I could use it?
 
Top Bottom