I'm fairly new to Drupal, and I am currently working on a Drupal user registration mechanism that involves authentication on a different website. The flow is as follows: - A user registers on a Drupal user/register form. Using user_register_submit() I set the the user's status to blocked. - After registering, the user is being redirected to a different website where he is required to log in. In that redirect, I pass the newly registered Drupal username as a parameter. If the login on that external site is successful, I'd like to redirect the user back to a .php page on the Drupal site (also sending back the username parameter via POST), and then change the status of that user to Active.

In order to achieve that, I've tried using the following code in a page called auth.php I placed under my theme's root directory. This code is failing:

    $username = $_POST['user'];
    $user_to_activate = user_load(array('name' => $username));
    user_save($user_to_activate, array('status' => 1);

After stumbling upon this http://drupal.org/node/820506, I realized core functions (such as user_load() and user_save() from the user.module) should be available from other modules. So I'd like to put my code inside a custom module, thing is I don't know in which function should I put it. Also, I wouldn't want this module to be called on every page load, only when being redirected from the external website I mentioned in the first comment.

Could someone offer some guidance?

3 Answers 3


In order to achieve that, I've tried using the following code in a page called auth.php I placed under my theme's root directory.

Normally in Drupal you don't create php files which are called externally. This can be done in some very rare cases. But most of the time you would define a menu callback in a module and have that call a function which you put your code in.

One of the issues that you have found is that Drupal is not bootstrapped from a generic PHP file.

It is fairly straightforward to do this.

  1. Create a module, with a .module file and a .info file.
  2. Turn on this module via the admin interface.
  3. In your module implement hook_menu() and provide a path to be called, in your example this could be 'mymodule/auth'.
  4. Create a function which will be called when your menu hook is fired.

There is a more in-depth tutorial here.

  • @Shoaib Nawaz Thank you for the quick and informative replies. Commented Jun 30, 2011 at 14:17
  • Here's my current code inside avner_module.module. I don't get errors but the user is still blocked. <?php function avner_module_menu(){ $items['groups/auth'] = array( 'page callback' => 'avner_module_page', 'page arguments' => 'avners', ); } function avner_module_page($username) { $account = db_fetch_object(db_query('SELECT u.uid FROM {users} u WHERE u.name = \'%s\'', $username)); $user_to_activate = user_load(array('uid' => $account->uid)); user_save($user_to_activate, array('status' => 1)); } Any ideas? Commented Jun 30, 2011 at 14:25

hook_menu Register a URL for your callback

function yourmodulename_menu(){
   return array(
      'your/desired/path' = array(
           'title' => t('page title'),
           'description' => t('page description'),
           'page callback' => 'your_callback'
           'access arguments' => array('access content'),
           'type' => MENU_CALLBACK,

Now define your function that will be invoked upon accessing your/desired/path

function your_callback(){
     //Put your code here OR include your script

The code will go in your custom module here is guide to create a module.


Here's the solution that worked for me, eventually, if anyone stumbles upon this post. I have indeed put my code inside a custom module, but i didn't implement hook_menu(). That's because menus are getting cached by Drupal and hook_user() won't be called if already cached.

As described above and here Getting the $user object of a logged out user based on a username and setting its status, I needed to fetch the $user object based on a username and change the user's status but only when hitting a specific page. So here's the very straightforward code to do just that (put inside mymodule.module):

  function mymodule_user($op, &$edit, &$account, $category = NULL) {
    switch ($op) {
      case 'insert' : {
        // set user status to blocked upon registration
        user_save($account, array('status' => 0));

  function mymodule_init() {
    // execute the following only if you're on http://www.example.com/activate
    if (drupal_get_path_alias(implode('/',arg())) == 'activate') {
      $account = user_load(array( 'name' => $_REQUEST['user'] ));
      user_save($account, array('status' => 1)); 

The 'activate' page access is restricted to a list of IPs and the page itself is requested through a remote http request via POST.

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.