4

Currently my Drupal installation is at www.example.com/wiki/

I was wondering if it is possible in Drupal 8.2.4 to access the current user uid from an external php script.

The script is located at www.example.com/someScript.php

I've tried this before

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

global $user;

if ($user->uid) {
    print "Logged in";
} else {
   print "Logged out";
}
5
  • The boostrap code you have is for Drupal 7. Try this one: drupal.stackexchange.com/questions/174474/… – Parag Dec 29 '16 at 4:46
  • @WellEndowedPenguin: you can try using Rest Services to get user login status, if that works. – Yogesh Dec 29 '16 at 6:18
  • @Yogesh Can I get login status based off only the drupal username/id? – WellEndowedPenguin Dec 29 '16 at 6:25
  • I'm not exactly sure about this. Checkout this documentation for default available Rest Services. – Yogesh Dec 29 '16 at 7:09
  • @WellEndowedPenguin, if you external script will be located into Drupal Root Directory, you can use the code sample provided by MrD in below ans. – Yogesh Dec 29 '16 at 7:15
3

You can following file authorize.php to do it like this:

<?php
use Drupal\Core\DrupalKernel;
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

$autoloader = require_once 'autoload.php';

function authorize_access_allowed(Request $request) {
  $account = \Drupal::service('authentication')->authenticate($request);
  if ($account) {
    \Drupal::currentUser()->setAccount($account);
  }
  return Settings::get('allow_authorize_operations', TRUE) && \Drupal::currentUser()->hasPermission('administer software updates');
}

try {
  $request = Request::createFromGlobals();
  $kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod');
  $kernel->prepareLegacyRequest($request);
}
catch (HttpExceptionInterface $e) {
  $response = new Response('', $e->getStatusCode());
  $response->prepare($request)->send();
  exit;
}

authorize_access_allowed($request);
$user = Drupal::currentUser();

if ($user->id()) {
    print "Logged in";
} else {
   print "Logged out";
}
6
  • 1
    @MrD, User sessions/Details are not accessible outside of Drupal. – Yogesh Dec 29 '16 at 6:16
  • 1
    @MrD, There is one thing I've notice is code in authorize.php is working for authentication because the authorize.php is located inside Drupal Root directory. If we move that out of Drupal Root directory, it fails the authentication. – Yogesh Dec 29 '16 at 7:14
  • 2
    Well, authentication is tied to the domain, the session cookie will not work outside of it, that's correct. If it's a subdomain, then you can configure a cookie_domain in settings.php, but separate domains can not work. – Berdir Dec 29 '16 at 7:32
  • 2
    The solution mentioned above worked, I placed the his code in the Drupal directory and then from my other directory I make an ajax call to the script! – WellEndowedPenguin Dec 29 '16 at 17:17
  • 1
    So, it is necessary that the script be at the same level as Drupal Root directory? cannot be placed inside a module? – Gnuget Apr 23 '18 at 20:38

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