SIGN UP MEMBER LOGIN:    
ARTICLE

Track Last Login of a WebSite Visitor

Posted by Abhimanyu Kumar Vatsa Articles | ASP.NET Programming May 23, 2011
In this article you will learn the best way to track and update the last login date and time of a site visitor.
Reader Level:

Introduction

 

Some web site administrators need to track user's last login date and time. There may be many solutions for this like updating the database through a landing page or a user's profile page or even any other page where the user lands first.

 

Even I have used a couple of ways for this but finally found a time mechanism. Maybe this is not too good but it is the best for me.

 

If we write some code in code-behind for this, then the query will execute each time the user navigates to the web site; this will increase the site response time. So, what's now? Let's take a look at my idea.

 

Sample Database

 

Look at the sample database table named 'users' having user account details like username, password, joining_date, last_login_date etc.

 

image002.jpg
 

Follow the steps:

 

Step 1

 

Add a Global.asax file in your web project.

 

Step 2

 

Add the following namespaces in Global.asax.

 

<%@ Import Namespace="System.Data.SqlClient" %>

<%@ Import Namespace="System.Data" %>

 

Step 3

 

Place the following code in 'Session_Start()' event.

 using (SqlConnection connection = new SqlConnection(GetConnectionString()))
        {            string dateandtime = DateTime.Now.ToString();            string loggedusername = HttpContext.Current.User.Identity.Name;
           
string sql = "UPDATE users SET last_login_date=@dateandtime WHERE username=@loggedusername";            connection.Open();            SqlCommand cmd = new SqlCommand(sql, connection);
           
SqlParameter[] pram = new SqlParameter[2];
           
pram[0] = new SqlParameter("@dateandtime", SqlDbType.VarChar, 49);
           
pram[1] = new SqlParameter("@loggedusername", SqlDbType.VarChar, 49);

            pram[0].Value = dateandtime;
           
pram[1].Value = loggedusername;

            for (int i = 0; i < pram.Length; i++)
           
{
               
cmd.Parameters.Add(pram[i]);
           
}
           
cmd.ExecuteNonQuery();
       
}

 

Step 4

 

Define the 'GetConnectionString()' function.

         public string GetConnectionString()
       
{

            return System.Configuration.ConfigurationManager.ConnectionStrings["ConnStrName"].ConnectionString;

        }

 

That's all about the sample update of the last login date and time of a user in the database without effecting speed of website.

 

HAVE A HAPPY CODING!!

Login to add your contents and source code to this article
share this article :
post comment
 

nothing, just asked... it's ok

Posted by Suthish Nair May 23, 2011

because, i need to execute my query once in a single session. am i wrong, plz reply?

Posted by Abhimanyu Kumar Vatsa May 23, 2011

why we need Global method for that?

Posted by Suthish Nair May 23, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Gauge for SharePoint
Become a Sponsor