Logging page referrer from within Global.asax

Posted on 2006-04-23
Last Modified: 2008-02-01
I've got an extremely basic Global.asax file which records visitor information on each Session_Start event.  The file uses an insert statement to add a record to a database each time a new session is started.  My code for the insert statement is below.

strInsert                = "Insert Into TBL_SESSION_LOG ( SessionID, StartTime, EndTime, ElapsedTime, PageCount, State, Referer, InitialRemoteHost, InitialRemoteAddress, UserAgent )"

 strInsert                = strInsert & " Values ( '" & Session.SessionID & "', #" & Now() & "#, #" & Now() & "#, 0, 0, True, '" & Request.ServerVariables("http_referrer") & "', '" & Request( "REMOTE_HOST" ) & "', '" & Request( "REMOTE_ADDR" ) & "', 'fred' )"

The values it returns all seem okay except for the final two.
Referer ---> Request.ServerVariable("http_referrer")
UserAgent ---> 'fred'

(The second one is a pretty obvious error!)

Although this code does not generate any errors when run, it does not seem to be recording any values for Referer. HOW CAN I MODIFY MY CODE TO ENSURE 'REFERRER' AND 'USERAGENT' VALUES ARE RECORDED?  I believe I read somewhere that these values are generated per page returned from the site- not sure if Global.asax is a special exception which needs handling differently.
Question by:Beamson
    LVL 25

    Accepted Solution


    UserAgent should be in Request.ServerVariables("HTTP_USER_AGENT") and referrer should be in Request.ServerVariables("HTTP_REFERER") -- notice the spelling on it one 'R'.

    Neither though are guaranteed to be present.  And, REFERER will typically only be present if it was from a link of another page.
    LVL 1

    Author Comment

    The suggestions work. (I hadn't bothered trying the HTTP_USER_AGENT one because my HTTP_REFERRRRRRER one hadn't been working)  I had the right idea - just bad spelling!

    Thanks for spotting it clockwatcher!

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now