0

I am in the process of building a drupal module that will block access to certain IP addresses, i've built the whole thing and it works a treat except from one area of functionality; i would like to be able to redirect blocked users to a node.

The code that i have does everything except display the node:

if (variable_get('lock_denied_response_type') == 2){
  $node_id = variable_get('lock_denied_node');
  $node = node_load($node_id);
  $node_view = node_view($node, FALSE, TRUE);
  drupal_render_page($node_view);
  exit;
}
else{
  echo variable_get('lock_denied_html');
  exit;
}

I've scanned google, stack overflow etc but not much luck. So far as i can tell, it appears my approach is wrong and rather than triggering my own render page, i should just change which node the standard rendering process uses, something like this:

if (variable_get('lock_denied_response_type') == 2){
  $node_id = variable_get('lock_denied_node');
  /* insert awesome node override code here */
}
else{
  echo variable_get('lock_denied_html');
  exit;
}

Suggestions?

Cheers!

2

you can do somthing like this:

function MY_MODULE_init() {
    global $user;

    // or you can also use ip_address() function
    if($user->hostname == 'MY_BLOCKED_IP') {
      drupal_goto('node/NID_OF_IP_BLOCKED_NODE');
    }
  }

2-aproach (this is a recommended way. Because hook_init() will be removed from drupal v8):

// in template.php
function MY_THEME_preprocess_page(&$variables) {
  if($variables['user']->hostname == 'BLOCKED_IP') {
    drupal_goto('node/NID_OF_IP_BLOCKED_NODE');
  }
} 
  • awesome, the node/ bit was the winner difference between two great answers :) thanks! – mister koz Dec 17 '13 at 6:55
  • @misterkoz See my updated answer. I showed a better solution. – xurshid29 Dec 17 '13 at 6:57
1

httpbl has a similar concept and it seems that it's using hook_boot.

So you might have something similar to:

/**
 * Implements hook_boot()
 */
function example_boot() {
  $result = example_check_ip(); // Check IP immediately upon access
  if ($result) {
    if ($_GET['q'] == '<URL_TO_NODE_YOU_WANT_TO_DISPLAY_IF_IP_IS_BLOCKED') {
      return;
    }

    drupal_goto('<URL_TO_NODE_YOU_WANT_TO_DISPLAY_IF_IP_IS_BLOCKED');
  }
}

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.