1

With a script on an external webpage (same domain), I need to detect whether the user that's visiting the page is or not logged in.

[EDIT] This occurs when the script is into a subfolder. If it's put into the root folder, it works fine. [/EDIT]

<?php
define('DRUPAL_ROOT',$_SERVER['DOCUMENT_ROOT']);
require_once DRUPAL_ROOT.'includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
global $user;

if(user_is_logged_in()){
    echo "logged";
} else {
    echo "not logged";
}
?>

despite the fact I'm logged-in, it returns always I'm logged out. Var_Damping $users, I get this

object(stdClass)#17 (4) {
  ["uid"]=>
  int(0)
  ["hostname"]=>
  string(12) "XXX.XXX.XX.X"
  ["roles"]=>
  array(1) {
    [1]=>
    string(14) "anonymous user"
  }
  ["cache"]=>
  int(0)
}

where XXX.XXX.XX.X is the IP of the server and I should be logged in as Administrator (user 1 )

  • I guess $user never knows which user it's supposed to be and so the session is initiated with just an unauthenticated user. I guess you need to have some endpoint that receives some user identifier (mail, username), load the $account from it and check if this user has started a session somehow, then let you return TRUE or FALSE accordingly, maybe as an uncached JSON response. – leymannx Jul 23 at 11:01
  • Or you go for some SSO/OAuth solution to have the user logged in externally from where you can programmatically log them in into Drupal. – leymannx Jul 23 at 11:04
  • @leymannx to load $account should use user_load_by_name($user_name) which needs credentials. The goal is to check if the user is loggedin, avoinding him to re-enter login and pwd ... – Tormy Van Cool Jul 23 at 11:18
  • No, you just need a user ID, or some other unique identifier to get the user ID from. Maybe this one helps: drupal.stackexchange.com/a/56405/15055 and api.drupal.org/api/drupal/modules!user!user.module/function/… – leymannx Jul 23 at 11:28
  • Well, basically the user should login into drupal (via its login page) than should have access to this ""outside"" script. Thus: the should detect whether the user is logged in or not, without giving credentials or else. – Tormy Van Cool Jul 23 at 12:27

Your Answer

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

Browse other questions tagged or ask your own question.