4

Here is my scenario:

I create a new user and dynamically create for him some content on which he is the owner.

Each user can log in, see his content and edit his profile. I use a block in which I have a menu links to each content and to his profile.

The theme output of the block function uses global $user; to get the current logged user.

However, the strange thing is that $user->uid return the last created user in the system and not the current logged user. Also the $user->language is of the last created user in the system. Why?

The user logs out with user/logout; this is the command to log out, right? Do I need to do more after user logout such as GLOBAL variable resets?

5

I would check to see that the block is not getting cached, or that you are not using something like Boost (static caching).

Sounds like the block contents are being cached to me.

  • Thanks for the hint. It is true, I have checked and Cache blocks was enabled. So this mean that I can not use this function in my case. – Ek Kosmos May 9 '11 at 20:11
  • 2
    It just means that you need to set the correct cache information for your block in hook_block_info, see api.drupal.org/api/drupal/modules--block--block.api.php/…. You probably want to use DRUPAL_NO_CACHE since a per user cache is often undesired (because a lot of cache entries are generated). Note that while in D7 this should automatically be updated when you change the code, it does not yet in D6: drupal.org/node/235673 – Berdir May 9 '11 at 22:46
3

global $user should ALWAYS refer to the currently logged in user.

There is a good chance that you are probably resetting the global $user variable in your custom code. A good practice when dealing with user accounts is to use the $account variable to distinguish between the two, see user_save() for an example.

  • 2
    There are cases where you must temporarily replace the global $user object. For example, when trying to use filter_access() in D6 for a different than the currently logged in user since there is no argument to specify account you want to use. See drupal.stackexchange.com/questions/315/… for how to do that. Anyway, it looks like the issue in this case are simply wrong/missing block cache settings. – Berdir May 9 '11 at 22:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.