4

While importing from an external app, I'm creating new users, and all their categories, subcategories, and articles. To get proper ownership of all the content, I'm logging the new user in, completing the import for their content, then logging them out. All the credentials work when tested from the login page of the site.

This works just fine, with the first login working as expected. However, the logout appears to be "missing" something related to the session because a duplicate entry error in #__session occurs the next time the ->login() is attempted.

$app = JFactory::getApplication();
foreach($imports as $import) {
    $credentials = array();
    $credentials['username'] = $import->username;
    $credentials['password'] = 'abc123';
    $app->login($credentials))        // Error occurs here the 2nd time this executes
        ...
        ... all the importing work that works as expected.
        ...
    $app->logout;   // Logout completes
}

This error occurs in application\cms.php, function checkSession` when it tries to insert the new session:

Duplicate entry '' for key 'PRIMARY' SQL=INSERT INTO 'xyz_session' ('session_id', 'client_id', 'guest', 'time', 'userid', 'username') VALUES ('', 0, 1, '0', 0, '')

Clearly, there's more to the session than just logging out, but after tinkering with $session for awhile, I can't find the recipe to prevent the error from occurring and allowing the next login to take place.

1

It all seems to be tied to guest users on the site. Adding the below code after logging out solved the problem:

$app->logout();
$session =& JFactory::getSession();
$session->destroy();
$session->start();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.