1

Background

I'm attaching the user register form, to the node create form with hook_form_alter. that way I create a node and user at once.

In order to validate and register the user, in hook_form_alter I add the relevant functions to my node form submit and validation calls.

function mymodule_form_alter form_alter(&$form, &$form_state, $form_id) {
   ....

    $form['#validate'][] = 'user_account_form_validate';
    $form['#submit'][] = 'user_register_submit';

Problem

The node is created and the user is created but the user isn't logged in.

  • Why isn't the user logged in?

The user register and login code seems to be running ok (see below), I can only think somehow the node creation is logging the user out? I have no idea how to continue debugging this.

Debugging

Thanks to Felix, I was able to debug by logging dpm (because it doesn't appear on the page) and the code is running correctly.

user_login_submit is called from user_register_submit

function user_login_submit($form, &$form_state) {
  global $user;
  $user = user_load($form_state['uid']);
  $form_state['redirect'] = 'user/' . $user->uid;
  user_login_finalize($form_state);
}

And then user_login_finalize is called.

function user_login_finalize(&$edit = array()) {
  global $user;
  watchdog('user', 'Session opened for %name.', array('%name' => $user->name));
  // Update the user table timestamp noting user has logged in.
  // This is also used to invalidate one-time login links.
  $user->login = REQUEST_TIME;
  db_update('users')
    ->fields(array('login' => $user->login))
    ->condition('uid', $user->uid)
    ->execute();

  // Regenerate the session ID to prevent against session fixation attacks.
  // This is called before hook_user in case one of those functions fails
  // or incorrectly does a redirect which would leave the old session in place.
  drupal_session_regenerate();

  user_module_invoke('login', $edit, $user);
}

I printed out $edit and $user and $edit is $form_state and $user is the newly created user. I can't see anything going wrong. The session opening is logged by watchdog, however perhaps it's never assigned?

How else could I continue to debug this? Could drupal_session_regenerate() be logging me out?

EDIT: I switched the user_register_submit to logintoboggan_user_register_submit, which worked on my dev environment but then broke on production with the same problem as above.

It doesn't appear to be a code problem, but something to do with the database or a setting. I dropped my production front end in front of my dev backend and it works fine, so it's some sort of database setting (which could of course effect the front end code).

3 Answers 3

2
+100

If a redirect is happening after print('x') then it will be lost. dmp() should show up however I prefer using Devel Debug Log in these instances so you know if then code is running then it will output to the log.

You can use the function ddl('log message') or ddl($array) and then check the report of what has been output once your code has run.

I realise this isn't actually answering your question but may make debugging easier...

3
  • Hmm that's really interesting. It could be perhaps they're lost somehow. Cheers, I'll go give this a shot now! Commented Feb 21, 2014 at 15:44
  • Cheers, that debugging was super useful. Turns out the code is running, and for some reason the login doesn't work. Not sure why because the correct functions are running. But that was super useful in getting this far. Commented Feb 22, 2014 at 14:33
  • Well Alexei didn't turn up, so have some bounty. Thanks for the super useful debugging tip! Commented Mar 2, 2014 at 14:47
2

Well the minute I put up a bounty I spot the answer. The problem was the switch between HTTPS and HTTP was killing my session. I'd accidentally secured the URL Alias not the actual URL.

Spotted it from another bountied question answer by @Alexei Reyu so waiting to see if he'll come and put his answer up here.

1
  • something else you can do so that your session won't die between the protocal switch is to set $conf['https']=TRUE; in your settings file. See drupal.org/https-information If you want to support mixed-mode HTTPS and HTTP sessions open up sites/default/settings.php and add $conf['https'] = TRUE;. This enables you use the same session over HTTP and HTTPS both Commented Mar 9, 2014 at 21:18
0
       1.  https://drupal.org/node/1545970
       2.  http://drupal.org/node/910598  

It is may be problem of https and http . check session variables is present or not.Please check this both ulr. it is may be help to your output

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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