【问题标题】:How to access user object in external PHP file?如何访问外部 PHP 文件中的用户对象?
【发布时间】:2018-03-21 10:47:27
【问题描述】:

我正在开发 http://example.com/userfiles/upload.php 作为 Drupal CKEditor 的文件浏览器,我想限制对登录用户的访问,但 upload.php 中的 $user->uid 似乎没有返回任何值。

我必须将路径 /userfiles 添加到 Apache 的重写规则异常中才能访问它(否则,它会将其转发到 index.php?q= 并且不会加载)。

我还不完全了解 Drupal 的 index.php 是如何工作的,但我怀疑 Apache 的重写规则可能是访问 $user->uid 值的问题?

或者我是否需要额外的代码才能从 Drupal 访问这个值?

$global user;

if (!$user->uid) {
  echo("You're not allowed to upload files");
  die(0);
}

【问题讨论】:

    标签: php apache mod-rewrite drupal drupal-7


    【解决方案1】:

    我以前搜索过很多,但现在我遇到了一个对我有用的post about how to bootstrap Drupal。我知道它提供了对 Drupal API 的访问。

    // Set the working directory.
    chdir('/var/www/html/drupalfolder/');
    define('DRUPAL_ROOT', getcwd());
    
    // Load Drupal.
    require_once './includes/bootstrap.inc';
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); 
    
    // Your code will go here ...
    

    【讨论】:

      猜你喜欢
      • 2013-08-17
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      • 2015-09-22
      • 2022-10-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多