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


global $user;

if ($user->uid) {
    print "Logged in";
} else {
   print "Logged out";
  • 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

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

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) {
  return Settings::get('allow_authorize_operations', TRUE) && \Drupal::currentUser()->hasPermission('administer software updates');

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

$user = Drupal::currentUser();

if ($user->id()) {
    print "Logged in";
} else {
   print "Logged out";
  • 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.