0

I have drupal cms and another php site with each own session and db etc on same root.

What I want is to pass somehow the drupal $user variable with all the login details of the user to the other non-drupal php site in order to bypass the login and make the user logged in there too.

The non-drupal site has its own login/register and sessions etc. I want to override this by getting the user info from drupal registration.

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

I tried to use the above code on the index of the non-drupal site, but I get errors about session and headers that already sent.

Any help?

1
  • 2
    Have you tried just bootstrapping the database, i.e. drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);?
    – Clive
    Feb 21, 2012 at 13:15

2 Answers 2

1

I have a similar setup running; what I did was sending the session ID as a URL parameter, including the "bootstrap.inc" file, bootstrapping the database, and then looking up the info I needed.

Example where I've stored some info in the session:

require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

if (@$_GET["sid"]) {
  $_SESSION["sid"] = @$_GET["sid"];
}

if (@$_SESSION["sid"]) {
  //Get the get username/role from the database
  $result = db_query("SELECT session FROM {sessions}
    WHERE uid > 0 and sid = '%s'", array(
      $_SESSION["sid"],
    )
  );

  while ($row = db_fetch_object($result)) {
    $_SESSION += unserializesession($row->session);
  }
}

function unserializesession($data) {
  if (strlen( $data) == 0) {
    return array();
  }

  // Match all the session keys and offsets.
  preg_match_all('/(^|;|\})([a-zA-Z0-9_]+)\|/i', $data, $matchesarray, PREG_OFFSET_CAPTURE);

  $returnArray = array();
  $lastOffset = null;
  $currentKey = '';

  foreach ($matchesarray[2] as $value) {
    $offset = $value[1];

    if (!is_null($lastOffset)) {
      $valueText = substr($data, $lastOffset, $offset - $lastOffset);
      $returnArray[$currentKey] = unserialize($valueText);
    }

    $currentKey = $value[0];
    $lastOffset = $offset + strlen($currentKey) + 1;
  }

  $valueText = substr($data, $lastOffset);
  $returnArray[$currentKey] = unserialize($valueText);

  return $returnArray;
}
0
0

If you call drupal_bootstrap() for the DRUPAL_BOOTSTRAP_SESSION or later phase, Drupal will override PHP session handling and starts a session in drupal_session_initialize(). When starting a session, Drupal will invoke session_start() which has already been invoked from your own separated non-drupal code.

If you only need to retrieve whether or not the user is logged in in Drupal, you can bootstrap the Drupal database only (ie. drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE)) and query the sessions table with the session id returned by session_id(). If the returned row has a non-zero uidm then you should have a logged in user. Session handling in Drupal is more complex than that, so obviously it won't be as robust.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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