[Drupal] How to save user IP address during user registration?

| | 4 min read

In this article will be explaining how to store user IP address to a custom table during registration and display the result using views.

While working on a Drupal site I came across a requirement to display list of all users in site with their name and IP address. We can implement this using Statistics module and views but unfortunately Statistics module makes the site heavy so its recommended not to use this module.

Next step to implement this feature is by creating a custom module. Here I am not mentioning about the basic steps in creating a custom module. Lets create an .install file for our custom module. Install file helps us define our custom database table name and its fields using hook_schema.

/**
 * Implementation of hook_schema().
 */
function custom_module_schema() {
  $schema['custom_table'] = array(
    'description' => 'The table that stores user id and IP address during registration.',
    'fields' => array(
      'custom_table_uid' => array(
        'description' => 'User ID.',
        'type' => 'int',
        'not null' => TRUE,
      ),
      'custom_table_username' => array(
        'description' => 'Username',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
      'custom_table_hostname' => array(
        'description' => 'Hostname',
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
      ),
    ),
    'primary key' => array('custom_module_uid'),
  );
  return $schema;
}

Next step is to implement the function custom_module_update_7000 in .install file, this function will helps us to create a custom table and its fields when update.php is accessed.

/**
 * Implementation of hook_update_N()
 * Called to Create table 
 */
function custom_module_update_7000() {
  db_create_table('custom_table', drupal_get_schema_unprocessed('custom_module', 'custom_table'));
  return 'Add the custom_table table for the Custom module.';
}

Once the custom table and its fields are created, lets make some data. To get the user details during registration lets use the function hook_user_insert. In this function we could get the user details like username, user id and IP address from the variable $account and store the details to our custom table.

/**
 * Implements hook_user_insert().
 */
function custom_module_user_insert(&$edit, $account, $category) {
  $user_id = $account->uid;
  $user_name = $account->name;
  $ip_address = $account->hostname;
  $user_data = array(
      'custom_table_uid' => $user_id,
      'custom_table_username' => $user_name,
      'custom_table_hostname' => $ip_address,
    );
  db_insert('custom_table')->fields($user_data)->execute();
}

Next step is to implement hook_views_api, in this function will define our includes file path name.

/**
 * Implements hook_views_api().
 */
function custom_module_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'custom_module') . '/includes/views',
  );
}

Next step is to create a views.inc file in path '/includes/views' folder.

To define our custom table values in views lets implement hook_views_data.In this function will relate our custom table values to users table and display all the required fields.

/**
 * Implements hook_views_data().
 */

function custom_module_views_data() {

  $data ['custom_table']['table']['group'] = t('Custom Module Tables');
  $data ['custom_table']['table']['join'] = array(
	  // Index this array by the table name to which this table refers.
	  // 'left_field' is the primary key in the referenced table.
	  // 'field' is the foreign key in this table.
	  'users' => array(
      'left_field' => 'uid',
      'field' => 'custom_table_uid',
    ),
	);
  $data ['custom_table']['custom_table_uid'] = array(
    'title' => t('User ID'),
    'help' => t('Custom User ID.'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    //Filter handler for filtering records by custom_module_uid
    'filter' => array(
      'handler' => 'views_handler_filter_numeric'
    ),
    'sort' => array(
      'handler' => 'views_handler_sort'
    ),
    'relationship' => array(
      'base' => 'users', // The name of the table to join with.
      'base field' => 'uid', // The name of the field on the joined table.
      // 'field' => 'nid' -- see hook_views_data_alter(); not needed here.
      'handler' => 'views_handler_relationship',
      'label' => t('Default label for the relationship'),
      'title' => t('Title shown when adding the relationship'),
      'help' => t('More information on this relationship'),
    ),
  );
  $data ['custom_table']['custom_table_username'] = array(
    'title' => t('Username'),
    'help' => t('Username'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string'
    ),
    'sort' => array(
      'handler' => 'views_handler_sort'
    )
  );
  $data ['custom_table']['custom_table_hostname'] = array(
    'title' => t('Hostname'),
    'help' => t('Hostname'),
    'field' => array(
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string'
    ),
    'sort' => array(
      'handler' => 'views_handler_sort'
    )
  );

  return $data;
}

/**
 * Implements hook_views_data_alter().
 */
function custom_module_views_data_alter(&$data) {
  // Adding relationship to the node table.
  $data['users']['custom_module_rel'] = array(
    'group' => t('Custom Module tables'),
    'title' => t('Custom Modules Users relationship'),
    'help' => t('Custom table relationship.'),
    'relationship' => array(
      'base' => 'custom_table', // Table we're joining to.
      'base field' => 'custom_table_uid', // Field on the joined table.
      'field' => 'uid', // Real field name on the 'node' table.
      'handler' => 'views_handler_relationship',
      'label' => t('Custom Module Users'),
    ),
  );
}

Now our custom table values will be displayed in views field. Hope you get the idea.

If you have any doubts regarding the above, please feel free to get in touch with us.