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.
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');
}
}