2

I came across this post: execute a php code after a user login and I implemented the accepted solution. In my implementation, however, I'm trying to output content to the page. This simply doesn't show up. My module name is "alert", and here's the code:

alert.module

<?php

function alert_user_login(&$edit, $account) {
  for ($i=0; $i < 1000; $i++) { 
    echo 'hi<br>';
  }
}

alert.install

<?php

function alert_install() {
  // Set the module weight so it can override other modules.
  db_query("UPDATE {system} SET weight = 99 WHERE name = 'alert'");
}

function alert_uninstall() {
  // Remove the module from the system table
  db_query("DELETE FROM {system} WHERE name = 'alert'");
}

alert.info

; $Id$
name = Alert
description = Present dialogs or perform redirects based on certain events
core = 7.x

I'm implementing this in my theme's template.php file. Should this work?

4

It will not work in template.php You need to add this in a module file, check here on howto instead of myTheme it will be modulename If module name is foo then it will be foo_user_login

    // @file : foo.module
    function foo_user_login(&$edit, $account) {
      fluenz_user_function($account->uid);
    }
    // and this function name was also different just an FYI
    function fluenz_user_function($uid) {
      for ($i=0; $i < 1000; $i++) { 
        echo 'hi<br>';
      }
    }
  • Ok, I've placed it in a module and updated my code above accordingly. Still not working (and yes, I have enabled the module). Nothing seems to happen at all. – maxedison Mar 2 '12 at 14:28
  • hook_user_login IS getting called because I was able to successfully log a watchdog message in my alert_user_login() method. But I don't see the hi<br>'s on the page. – maxedison Mar 2 '12 at 14:35
  • As destination page is different you wont be able to see all the echo s, but if you put a die(__FUNCTION__) after for loop you will probably see that it halted right after all echo statements. – GoodSp33d Mar 5 '12 at 4:29
  • Use drupal_set_message() instead of echo. It will preserve messages across page redirects. See api.drupal.org/api/drupal/includes!bootstrap.inc/function/… – diamondsea Jan 5 '16 at 13:10
1

hook_user_login(), and hook_user_logout() are not thought to be used to alter the content of a page. They are thought to record the fact a user logged in, or logged out, and eventually set a message with drupal_set_message(). To notice that the answer for the question you are referring is talking of executing code, not printing a string through print(), or echo().

hook_user_login() could be used to set a global variable, or a static variable (see drupal_static()) that is retrieved from another hook.

For altering the content of a page, there are other hooks; which one you should use depends from what exactly you want to achieve.

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.