XF 2.1 Error: The session key must be overridden.

sajal

Active member
I have built this standalone script for logging in the user programmatically.

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$basePath = "/web/forum";

// Require the XenForo autoloader
require $basePath . '/src/XF.php';

// Initialize XenForo
XF::start($basePath);

// Set the user ID you want to log in
$userIdToLogin = 13; // Replace with the actual user ID

// Fetch the user
$user = XF::em()->find('XF:User', $userIdToLogin);

XF::app()->session()->set('sessionKey', 12345);
XF::session()->changeUser($user);
XF::setVisitor($user);

But I get the following error:

An exception occurred: [LogicException] The session key must be overridden. in src/XF/App.php on line 816
  1. XF\App->XF\{closure}() in src/XF/Container.php at line 28
  2. XF\Container->offsetGet() in src/XF/App.php at line 2506
  3. XF\App->session() in test_login_programatically.php at line 20
I would appreciate if anyone can help on this.
 
@Jeremy P

Thanks a lot for your reply.

Ok I have tried that. But, it's redirecting to the home page, and not executing the code below that.


<?php
error_reporting(E_ALL);
ini_set('display_errors',1);

$basePath = "/web/forum";

// Require the XenForo autoloader
require $basePath . '/src/XF.php';

// Initialize XenForo
XF::start($basePath);
XF::runApp('XF\Pub\App');

// Set the user ID you want to log in
$userIdToLogin = 13; // Replace with the actual user ID

// Fetch the user
$user = XF::em()->find('XF:User', $userIdToLogin);

XF::app()->session()->set('sessionKey', 12345);
XF::session()->changeUser($user);
XF::setVisitor($user);
 
Top Bottom