I need to check if the http_referrer var is a specific page in Drupal and redirect otherwise.

I was trying the Rules module but I need something flexible enough to add PHP code.

Can I use hook_init() in my template.php function to check for the referrer and the destination node and redirect if certain conditions are in place?


You can implement hook_init in a custom module.

function my_module_init() {
  // Since this file should only be loaded on the front page, it cannot be
  // declared in the info file.
  if (drupal_is_front_page()) {
    drupal_add_css(drupal_get_path('module', 'foo') . '/foo.css');

But note that this WILL NOT work if the page is cached or if you are using a revers proxy cache. If you need to do this on cached pages look into using hook_boot

That being said, there is often better ways to accomplish a redirect based on certain conditions. Update your question with more details and perhaps we can find more suitable solutions.

  • I'm going the javascript route, i'll update shortly. I know that the http_referrer var is not always available, at least not my D7 instance – AlxVallejo May 16 '14 at 16:06

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.