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]

require_once DRUPAL_ROOT.'includes/bootstrap.inc';
global $user;

    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) {
  string(12) "XXX.XXX.XX.X"
  array(1) {
    string(14) "anonymous user"

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 '19 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 '19 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 '19 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 '19 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 '19 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.