1

I am developing a module for Drupal which provides a login form and after submission user cURL to login in with those credentials on another website.

If all goes well, the module redirects the user to their profile and should present the information cURL fetched. This is where things go wrong.

What I'm trying to do is get the $account variable through the username. This is all in the module_name_form_submit function:

$account = user_load(array('name' => check_plain($user)));

And then append the data I want to display to it:

$account->content['module_name'] = array(
    '#title' => t('Module Title'),
    '#values' => get_info()
    );

If I do a print_r before the redirect, sure enough, the data is there as it should. But after the redirect:

$form_state['redirect'] = "users/{$user}";

I no longer have access to that same data. So the question is, how can I set that variable in a permanent way and access it after the redirect in the user profile page? (I'm trying to avoid sessions).

  • $account = user_load(array('name' => check_plain($user))); => check_plain() should be used when displaying user submitted content. It is not necessary here as it does not protect against SQL-Injection. Instead, make sure to use proper placeholders when doing your own queries (user_load() does that..). – Berdir May 30 '11 at 6:12
0

PHP is a "shared-nothing" architecture, nothing persists over multiple requests unless stored externally.

So you really only have two options:

  1. Sessions. Drupal already maintains a session for you for every user (also anon users, except on the first request). So just add your data to $_SESSION['yourmodule']['yourdata'] and you have it available on the next request where you can remove it again if you want.

  2. GET. Alternatively, you can also pass the data forward through the GET request, by doing the redirect like this: $form_state['redirect'] = array('user/' . $account->uid, array('key' => 'value')). As in my example, you should always use the internal URL when doing a redirect, Drupal will take care of the aliasing fory you.

  • You are correct, but I'm trying a third option which seems to be working. I'm using variable_set('var_name', the_array); and then I've implemented hook_user to load that variable when the user profile is viewed, like so variable_get('var_name');. Thanks :-) – brpinto May 30 '11 at 14:37
  • No, that is wrong. variable_set() results in a cache clear which will slow down your site and fills up the variables table (which is read completely on every page request, do not store large data in there). See stackoverflow.com/questions/6041471/… for details. – Berdir May 30 '11 at 14:40
  • but the data isn't that big (just some chars in a couple of array keys) and I do a variable_del right after the variable_get. I know it's a lot of sql queries but I want to avoid sessions or using GET, which leaves me with few options. – brpinto May 30 '11 at 14:51
  • Again, the problem is that variable_set/del result in cache clear which means Drupal will have to re-fetch all (possibily hundreds) of entries in the variables table and store them as a serialized array in the cache. Every. time. you. change. a. variable. If you really want to avoid sessions (why? this is imho a perfect use case for that) then store it in a custom table or something like that. – Berdir May 30 '11 at 15:12
  • yup, you're right. I guess I'll have to use sessions. Thanks :) – brpinto May 30 '11 at 16:08
0

I add several things to the user object in one of my sites. I use hook_user($op = 'load') and that has always been fine. Does that not satisfy your needs? Ouch - just noticed this is almost a year old. Sorry :)

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.