Jump to content

How can I get the $user of another PW instance?


Recommended Posts

I have 2 ProcessWire instances running under the same domain, each with own session name and cookie.

$config->sessionName = 'wire0';
$config->sessionName = 'wire1';

The assignment of the correct instance via url is done in index.config.php. One of the 2 instances is without frontend and accessible only under the related admin url. In this instance (wire0) the user with ID=14179 is logged in. If I check if the session of the related user in this instance is valid I get false. The user var returns unexpected results as well.

$xy = new ProcessWire($config->paths->root . 'site-xy/');
var_dump($xy->session->isValidSession(14179)); // false, should be true
var_dump($xy->user->isLoggedin()); // false, should be true

If  I use the $pages, the $users, $config or any other API of the $xy instance I get what I expect.

$userIDs = $xy->users->findIDs('check_access=0');
var_dump(count($userIDs)); // correct number of users in related instance
var_dump($xy->config->sessionName); // wire0
var_dump($config->sessionName); // wire1

How can I get the $user or $session vars of another PW instance?

Link to comment
Share on other sites

  • 1 month later...

Possible solution to set $user using a setup with multiple sites (multiple databases Option #1), module SessionHandlerDB and different session names for each instance. Put this code in your ready.php

$xy = new ProcessWire($config->paths->root . 'site-xy/');

 * if a session AND a challenge cookie is present we check if the user is still loggedin in xy
 * if yes we set the @var wire('user') to the xy instance
if ($xy->session->hasCookie() && $xy->session->hasCookie(true)) {

    // get xy session ID
    if ($xy>config->https && $xy->config->sessionCookieSecure) {
        $cn = $xy->config->sessionNameSecure;
            if (!$cn) $cn = $xy->config->sessionName . 's';
    } else {
        $cn = $xy->config->sessionName;
    $SID = $input->cookie->$cn;

    // set the wire('user') if loggedin
    if ($SID) {
        $SHDB = $xy->modules->getModule('SessionHandlerDB', array('noPermissionCheck' => true))->getSessionData($SID);

        if ($SHDB['user_id'] != 40) {

            // get fingerprint and session timestamp
            $fp = isset($SHDB['data']['Session']['_user']['fingerprint'])? $SHDB['data']['Session']['_user']['fingerprint'] : false;
            $ts = isset($SHDB['data']['Session']['_user']['ts'])? (int) $SHDB['data']['Session']['_user']['ts'] : 0;

            // validate fingerprint
            if ($xy->session->getFingerprint() !== $fp) {
                $this->session->error(__('Unable to assign XY user. XY-Session fingerprint does not match.'), Notice::debug | Notice::log);

            // check if session timed out
            else if ($xy->config->sessionExpireSeconds && $ts < (time() - $xy->config->sessionExpireSeconds)) {
                $this->session->error(__('Unable to assign XY user. XY-Session timed out.'), Notice::debug | Notice::log);

            else {
                $_user = $xy->users->get($SHDB['user_id']);
                if ($_user->id) {
                    // session valid, assign user & user_id will be set correctly in DB via SessionHandlerDB::write()


Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...