Last active
December 13, 2017 19:38
-
-
Save blackswansolutions/6361906 to your computer and use it in GitHub Desktop.
PHP: Xenforo Session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment