2

How can I determine if a user is hitting my site from elsewhere (i.e. from a new tab or another site)? I need to present a 'welcome back' message to returning users who are still logged in, but I don't want it appearing if they're just navigating within my site or, say, reloading the homepage.

I've considered using Onload/Onbeforeunload (or similar) to keep track of whether there is currently a view open, but this just adds complexity.

Can I assume that if Request.UrlReferrer doesn't contain my domain then it's a 'fresh' visit?

Gary Chapman
  • 418
  • 9
  • 20
  • You can't always rely on `Request.UrlReferrer` as it can be easily disabled, some browsers don't even send it. It also won't be set if the user returned to your site via a bookmark. – DGibbs Nov 19 '14 at 11:15
  • The referrer is the only information from the browser about the user's last page visited. But it is only what the user's browser supplies (ie. you cannot tell the browser "send me correct referrer", for example see https://addons.mozilla.org/en-US/firefox/addon/refcontrol/). However given that most of the time for most users the referrer will contain the last page visited for the current tab. Don't forget to validate "open link in new tab/window" and "duplicate tab" cases across common browsers). – Richard Nov 19 '14 at 11:15
  • 1
    @DGibbs How does one disable `Request.UrlReferrer`? Chrome, FF and IE all supply a null `Request.UrlReferrer` when I arrive from another site/new tab/bookmark, but when browsing within my site (including "open link in new tab/window") it contains the previous URL. Does this mean it'd be a safe bet to assume a null `Request.UrlReferrer` constitutes an external visit, or can it be disabled even when browsing within my own site? – Gary Chapman Nov 19 '14 at 14:27

1 Answers1

3

Try this,

if(Request.UrlReferrer != null && Request.UrlReferrer.Host != Request.Url.Host)
{
    // User visit your page from another site/domain
}