3

I have a website built on drupal. I want to log (preferably db log) all the calls being made to a drupal page. The drupal page is called as shown here

http://www.example.com/page-name?url=example2.com&redirect=example3.com&uid2=randomNumber

I need to log all URL fields (url,redirect,uid2) alongwith timestamp and drupal user id.

Whats the best way to do it?

2
  • Only calls to /page-name or calls for all pages on the site, that include this parameters?
    – user11153
    Mar 29, 2013 at 19:08
  • @user11153 only calls to /page-name including this parameters
    – lavesh
    Mar 29, 2013 at 19:20

1 Answer 1

2

Create a custom module, for example:

MODULENAME.install file with hook_schema() implementation:

<?php

/**
 * Implements hook_schema()
 */
function MODULENAME_schema() {
  $schema['MODULENAME'] = array(
    'description' => 'Description of the table.',
    'fields' => array(
      'timestamp' => array(
        'description' => 'Timestamp of the rquest.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      // definitions for the rest of fields here
    ),
    'indexes' => array(
      // define indexes
    ),
    'primary key' => array('NAME_OF_PRIMARY_KEY_FIELD'),
  );
  return $schema;
}

?>

MODULENAME.module file with hook_menu() implementation:

/**
 * Implements hook_menu()
 */
function MODULENAME_menu() {
  $items['page-name'] = array(
    'title' => 'Example title',
    'access callback' => TRUE, // imply no restrictions
    'type' => MENU_CALLBACK,
    'page callback' => 'MODULENAME_pagename_view',
  );
  return $items;
}

function MODULENAME_pagename_view() {
  global $user;
  // insert into Drupal database
  db_insert('MODULENAME')
    ->fields(array(
      'timestamp' => REQUEST_TIME,
      'user_id' => $user->uid,
      'url' => $_GET['url'],
      'redirect' => $_GET['redirect'],
      'uid2' => $_GET['uid2'],
    ))
    ->execute();
}

?>
4
  • Pay attention to access callback and title in your hook_menu implementation.
    – AKS
    Mar 29, 2013 at 21:40
  • @Ayesh K Good point, corrected.
    – user11153
    Mar 29, 2013 at 22:01
  • @user11153 how can i log it in external/internal database??
    – lavesh
    Mar 30, 2013 at 15:20
  • @lavesh I updated my answer.
    – user11153
    Mar 30, 2013 at 15:48

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.