doswy02440 2016-11-24 14:41
浏览 49

使用ini_set设置新变量

I'm trying to set value some custom variable for some web service calls based on user is logged in or not. I'm using following code, but it is not setting up the value of the variable.

function mymodule_user_login(&$edit, $account) {
    if ($account->uid > 0 ) {
      ini_set('saf.service_value', 'user-Auth');
  }
}

function mymodule_user_logout($account) {
  ini_set('saf.service_value', 'user-Anon');
}

Update #1 The web service is provided by third/forth party and we have no control over it. It allow us to set value in .htaccess file. Since we are using Apache 2.2, we are not able to use directive. With my findings so far, I think we can't set can't set custom variables using init_set(), we can only allow to set predefined variables only (Please confirm if it is correct, I didn't find any documentation/blog post that conform it). If I set the variable in .htaccess file as follows

php_value saf.service_value "user-Anon"

it works perfectly, I just want to set that value dynamically.

  • 写回答

1条回答 默认 最新

  • doujian4752 2016-11-25 10:56
    关注

    I am not quite sure why you want to do this, if the purpose was to deny access to some nodes if connected or not, there are many modules for that (node_access, ...).

    But as Mad Dog said init_set() is only working the time of your execution, wich mean, with your current solution, it will be set only when someone login or when someone logout, not when they will be exploring the site btw 2 login/logout.

    You want to use hook_init() (if you have no cache) or hook_boot() and test if the user is connected or not. So each time PHP is executed, it will be set.

    Something like that :

     /**
     * Implements hook_boot().
     */
     function mymodule_boot() {
      global $user;
    
      // if user is connected in Drupal
      if ($user->uid != 0) {
        ini_set('saf.service_value', 'user-Auth');
      }
      else {
        ini_set('saf.service_value', 'user-Anon');
      }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算