Login after external form creates user


Well-known member
I'm running around like a chicken with my head cut off ... or am I a dog chasing my own tail? Regardless, something is very wrong with my attempts to get a user created externally and logged into XenForo without the user intervening (register and login in one form).

<div id="xenregister">
   <form action="" method="post" id="pageLogin">
      <div class="ctrlWrapper">
         <dl class="ctrlUnit">
            <label for="RegisterControl">Preferred Username:</label><br />
            <input type="text" size="15" name="username" id="RegisterControl" class="textCtrl" tabindex="105" /><br />

            <label for="RegisterControl">Your email address:</label><br />
            <input type="text" size="15" name="email" id="RegisterControl" class="textCtrl" tabindex="106" /><br />

            <label for="ctrl_password">Password:</label><br />
            <input type="password" size="15" name="password" id="ctrl_password" class="textCtrl" tabindex="107" /><br />

            <input type="submit" name="registerButton" class="button primary" value="Register" tabindex="108" />
            <label for="ctrl_remember" class="rememberPassword">
               <input type="checkbox" name="remember" value="1" id="ctrl_remember" tabindex="109" /> stay logged in</label>
      <input type="hidden" name="cookie_check" value="0" />
      <input type="hidden" name="redirect" value="<?php echo $redirect ?>" />
      <input type="hidden" name="_xfToken" value="" />


if ( isset( $_POST["registerButton"] ) ) {

   // Build array
   $data = array( 'login' => $_POST["username"], 'password' => $_POST["password"] );

   // Create the XenForo User.
   $user_id = XenWord::createUser($_POST["username"], $_POST["email"], $_POST["password"] );

   $userModel = new XenForo_Model_User();
   $loginModel = new XenForo_Model_Login();
   $user = $userModel->getUserById( $user_id );


   $userid = $userModel->validateAuthentication($data['login'], $data['password'], $error);

   if (!$userid)
      return $error;


   if ($data['remember'])

   XenForo_Model_Ip::log($userid, 'user', $userid, 'login');

   // $userModel->deleteSessionActivity(0, $this->request->getClientIp(false));

   $session = XenForo_Application::get('session');

The user is created. The var_dump shows the user information, however, they are not logged in. Why can't there be a simple XenForo_Login and bang. Done. LOL.

Any suggestions on where I'm going wrong after creating the $user ?

I've tried many variations of the above but keep going in circles. Thank you for your guidance.


Well-known member
Does anyone have any suggestions? I've wasted hours on this. It just seems that the lines from the completeLogin controller would work.

if ( isset( $_POST["registerButton"] ) ) {

    $username = $_POST["username"];
    $email = $_POST["email"];
    $password = $_POST["password"];
    $additionalData = array();

    $writer = XenForo_DataWriter::create('XenForo_DataWriter_User');
    $writer->set('username', $username);
    $writer->set('email', $email);
    $writer->set('user_group_id', XenForo_Model_User::$defaultRegisteredGroupId);
    $writer->set('user_state', 'valid');
    foreach ($additionalData AS $data => $key) {
        $writer->set($data, $key);
    $userObject = $writer->getMergedData();

    $userId = $userObject['user_id'];
    // from completeLogin

    XenForo_Model_Ip::log($userId, 'user', $userId, 'login');

    $visitor = XenForo_Visitor::setup($userId);

    XenForo_Application::getSession()->userLogin($userId, $visitor['password_date']);


The var_dump returns the $visitor information for the newly created user but the user is not logged into XenForo.

Sorry. This is just very frustrating. It shouldn't be this difficult and it's probably something very obvious. :(