The method mentioned at Php Access User Data from external script works for me but only if the script is at the document root. If I move the file into my custom module the user returned is the anonymous user.

So, is there something else to do in order to keep all my files in my module and avoid starting to add custom scripts in the Drupal root folder?



The code snippet is bootstrapping Drupal by assuming it's in Drupal's root path (i.e. require_once 'autoload.php';) To have it runnable from the module dir, you'll have to alter the relative path to make it work from wherever your script is at (e.g. is your module is in DRUPAL_ROOT . 'modules/custom/my_module/scripts/my_script.php' you would have use the path ../../../../includes/autoload.php to reach it).

Depending on what you're doing, you might want to consider a custom Drupal Console/Drush command as a part of your module instead of a PHP script that has to assume/find the location for bootstapping Drupal.

  • Yeah in my script I changed the path of the autoload otherwise I got a fatal error. – Gnuget May 1 '18 at 18:53

Your Answer

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

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