Drupal - 使用 boostrap 在 Drupal 之外检查登录用户不起作用

2021-12-29 00:00:00 php bootstrapping drupal

我正在拔头发试图弄清楚这一点.在 Drupal 目录之外时,我无法让 Bootstrap 正常工作.如果我在 Drupal 目录中运行此代码,它可以正常工作,但上一级不起作用.

I'm pulling my hair out trying to figure this one out. I can't get the Bootstrap to work properly when outside of the Drupal dir. It works fine if I run this code in the Drupal dir, but up one level doesn't work.

我的 Drupal 路径是/public_html/drupal/.我正在运行的脚本在/public_html 中.

My Drupal path is /public_html/drupal/. The script I'm running is in /public_html.

$user 没有返回登录的用户.我已经确定这不是跨域问题(即 www.domain.com 与 domain.com).

$user is not returning the logged in user. I've made sure it's not a cross-domain issue (i.e. www.domain.com vs. domain.com).

chdir('/path/to/drupal');

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

if ($user->uid) {
    print "Logged in";
} else {
   print "Logged out";
}

推荐答案

如果 Drupal 在您的单独脚本中未识别用户,则很有可能它没有收到正确的会话 cookie.您应该检查正常 Drupal 安装设置的 cookie,看看它们是否也发送到您的脚本.如果没有,请检查 Drupals settings.php 中的 $cookie_domain 变量 - 通常它已被注释掉,但您可能需要在您的情况下明确设置它.

If the user is not recognized by Drupal in your separate script, chances are high that it does not receive the proper session cookie. You should check the cookies set by your normal Drupal install and see if they get send to your script as well. If not, check the $cookie_domain variable in your Drupals settings.php - ususally it is commented out, but you might need to set it explicitly in your case.

相关文章