Drupal: drupal_set_message doesnt display a messag

2019-04-28 20:24发布

问题:

I cannot seem to get a message from drupal_set_message when a user registers on my site. I'm using Drupal 6.14.

Adding a print in the user.module:

function user_register_submit($form, &$form_state) {
  ...
      if ($notify) {
        ...
      }
      else {
        drupal_set_message(t('Your password and further instructions have been sent to your e-mail address.'));
        print 'xxxxxx';
        $form_state['redirect'] = '';
        return;
      }
    }
    ...

}

It prints 'xxxxx';

A var_dump of the $_SESSION variable gives the status message, which drupal_set_message doesnt display, so that looks fine also.

I've uninstalled all my modules, only core remains, and using Garland now as a theme.

Furthermore, I've installed a fresh Drupal installation, and there it gives me a nice status message.

Then i compared my .htaccess and Drupal's, from the fresh install. Modified mine to make them equal.

Nothing helps.

Any thoughts?

回答1:

nicely done finding the cause - I had this exact same problem a couple of weeks or so ago.

Now, for the reason:

Drupal sessions are linked by ID number (you can see this in the session table in the database if you look) to the user. Drupal also has it's own session handling functions, and one of those is a check to see if the current session is associated with a valid user account - and for anonymous users, that is the user 0 - (it doesn't matter if multiple sessions are open per user - which is certainly what happens when so many anonymous users visit your site).

If Drupal does not find a valid user for the current session, then the session is regenerated anew - meaning the previous information is lost.

Edit:

A recent comment has prompted me to add a bit more depth to the answer as I have since found the most likely cause for the error.

Basically, it's the use of ID 0 for the anonymous user. If you INSERT a 0 value into an auto-incrementing field in MySQL, the value will actually become the next available value in the sequence (so, a table with auto-incrementing field set to 10 will INSERT at 11 and not 0).

We hit the problem because we were using MySQL dumps for exporting and importing backups during development.



回答2:

We just solved it. Apparently, user 0 was missing, somebody deleted it or some module did it. After inserting it into the database we got our messages again.

This is what you have to do:

INSERT INTO `users` (`uid`, `name`, `pass`, `mail`, `mode`, `sort`, `threshold`, `theme`, `signature`, `signature_format`, `created`, `access`, `login`, `status`, `timezone`, `language`, `picture`, `init`, `data`) VALUES
(0, '', '', '', 0, 0, 0, '', '', 0, 0, 0, 0, 0, NULL, '', '', '', NULL);

And after that set the uid to 0, because uid is an autoincrement!!

No idea still how that messus up with drupal_set_message, though.



回答3:

Another problem could be that your theme doesn't print $message in the templates.



回答4:

I couldn't get messages to show up even when doing theme_get_messages() or echo $messages etc.

So I had to get it from $_SESSION['messages'].

ie:

<?php 
            // we aren't getting messages, get them manually
            if (isset($_SESSION['messages'])) {
                echo '<div class="messages">';
                foreach($_SESSION['messages'] as $type=>$messages) {
                    echo "<p class=\"$type\">".implode("</p><p class=\"$type\">", $messages)."</p>";
                }
                echo '</div>';
                unset($_SESSION['messages']);
            }

        ?>

Hope that helps someone.



回答5:

i find this same for a long time, this is cause for other modules that you install that affect this you can apply this code in a phpmyadmin o in console if you use linux.

INSERT INTO `users` (`uid`, `name`, `pass`, `mail`, `theme`, `signature`, `signature_format`, `created`, `access`, `login`, `status`, `timezone`, `language`, `picture`, `init`, `data`) VALUES
(0, '', '', '', '', '', 0, 0, 0, 0, 0, NULL, '', '', '', NULL);


回答6:

drupal_set_message($msg, $type = 'status'); lets you set a message, it will be displayed automatically on the next (or current) page the user visits.

See the docs: http://api.drupal.org/api/function/drupal_set_message/6



标签: drupal