Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

track session time of user using Global.Asax?

Options
  • 07-03-2015 3:08am
    #1
    Closed Accounts Posts: 153 ✭✭


    I am building a simple online quiz. When the user submits their details and pressed the ' start quiz button ', I want to be able to track how long it takes for them to do the quiz. I understand you use the global.asax file and add a session to the application start method.

    When the user finishes the quiz they are then taken to a final page which tells them how long it took for them to complete the quiz.

    I just want to know do how do I go about creating a timer to track the users time on the quiz. I know I have to minus the timer from the current time to get the total amount of time spent on the website. Do i need to use session states ?


Comments

  • Registered Users Posts: 11,977 ✭✭✭✭Giblet


    A session is usually tracked in memory of the application, a cookie, or in the database. You just need to configure SessionState in the web.config, and you can begin using the Session object to store user specific variables, it usually creates a cookie to track the user automatically. HttpContext.Session is the man you're after, it can be called from within the first page code behind to set the time, and read on the last page to get the total time taken. The global.asax has some events around session state begin and end, but you don't really need them.
    Page1:
    Session["QuizStartTime"] = DateTime.UtcNow;
    
    FinalPage:
    var starttime = Session["QuizStartTime"] as DateTime;
    if(starttime != null)
    {
        var endtime = DateTime.UtcNow - starttime;
    }
    

    Or something to that effect.

    https://msdn.microsoft.com/en-us/library/h6bb9cz9%28v=vs.71%29.aspx

    Do you need to use Session state? Not really, a cookie could do the same thing, and personally, I just don't use it anymore, but for a simple app it will get the job done.


Advertisement