I need to hook into whenever a user moves from one page to another. I need to know where they are coming from and where they are going. My scenario is that anytime that a user heads to a certain section of the site (search) it modifies the path to add a variable. But I only want to to do so when they are coming from outside that section of the site.

I have been looking at hook_url_inbound_alter() which gets me in when a user is about to move to another page, and gives me where they are going, but not where they came from. Any suggestions?

  • 1
    Check HTTP_REFERER. Note that this cannot be trusted all time as this can be modified, if this doesnt affect you you can use this to get last visited page
    – GoodSp33d
    Mar 6, 2013 at 8:49
  • 2
    You misunderstand hook_url_inbound_alter. This hook allows you modify how links that link within your website are displayed. It does not tell you a user clicked on that link. Mar 6, 2013 at 9:32

1 Answer 1


Drupal does not track this information, so you need to do it yourself, using sessions (*).

If your pages are not being cached, then the best place to track user progress would probably be in hook_init. This is called very early on in the process - so in effect you have the information of knowing where the user wanted to go before anything is displayed (you could redirect the page at that stage for instance).

If your pages are being cached, in particular using a cache mechanism that bypasses Drupal such as Boost or an external cache proxy, then you will need a Javascript based solution that sends the information back to your site. How to implement this is beyond the scope of this answer.

(*) Note however that Drupal implements it's own session handling, so you need to do some extra work to use session variables (the session variables you set are only available through $_SESSION on the next page load - in the mean time you need to keep track of them yourself. Some people have written example implementations, or you may want to try the session api module).

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.