hook_init for node redirect 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.