【发布时间】:2012-11-11 02:27:11
【问题描述】:
我在访问外部页面上的 $_SESSION 变量时遇到问题。
基本上,我在一个drupal节点上有一个表单,当提交时,它会发布到一个外部php文件,该文件将值保存到一个$_SESSION变量,如下所示:
//Bootstrap Drupal so we can use the SESSION variables, where we store the calling number for the
//duration of the logged in session
define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
$base_url = 'http://'.$_SERVER['HTTP_HOST'];
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
//Save number as the calling number for the session
$_SESSION['calling-number']=$_REQUEST['calling-number'];
效果很好。
当我稍后尝试使用以下方法从外部页面访问 $_SESSION 变量时:
//Bootstrap Drupal so we can use the SESSION variables, where we store the calling number for the
//duration of the logged in session
define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);
$base_url = 'http://'.$_SERVER['HTTP_HOST'];
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_SESSION);
echo $_SESSION['calling-number'];
我什么也得不到。如果我查看这个新页面上的 $user,我可以看到这是因为它在匿名用户会话 (uid = 0) 下运行,而不是设置 $_SESSION 变量的登录用户 ID,因此它找不到。
任何想法为什么我不使用登录用户的会话?
编辑
不知道为什么会这样,但是只要两个外部文件位于不同的目录中,就可以正常工作。如果它们在同一目录中,则似乎开始了一个新会话而不访问现有会话。不知道为什么。
【问题讨论】:
-
您无需引导 Drupal 即可访问 $_SESSION。
-
刚试过没有引导。仍然没有运气 - 有什么想法吗?如果我删除引导程序,我将无法再访问 $user 或任何其他 Drupal 变量,因此看来我确实需要它。
标签: php session drupal drupal-7 session-variables