Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friends,

I want to show total users of the website who's currently login on the website.
i m searching the code on the Google to count total logged-In User but not found yet.i Need that code

thanking you
Gulam Hussain
Posted

This looks like a reasonable answer:
Tracking User Activity[^]

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 25-Mar-11 0:05am    
Very clear article referenced, my 5.
--SA
Espen Harlinn 25-Mar-11 4:38am    
Thank you, SAKryukov!
You need to use Session_Start and Session_End event handlers:

C#
public class Global : System.Web.HttpApplication
    {
        public static int OnlineUsers { get; set; }

        void Application_Start(object sender, EventArgs e)
        {
            OnlineUsers = 0;
        }
        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown
        }
        void Application_Error(object sender, EventArgs e)
        {
            // Code that runs when an unhandled error occurs
        }
        void Session_Start(object sender, EventArgs e)
        {
            OnlineUsers++;
        }
        void Session_End(object sender, EventArgs e)
        {
            OnlineUsers--;
        }
    }


And your code behind:

lblOnlineUsers.Text = "Online Users: "+ Global.OnlineUsers;


Please vote if my answer will help you.
 
Share this answer
 
v2
You Can use session variable.

Using Session Variable You Have To Count The The Number Of Online Users..

For That Use The Below Code :
C#
void OnlineUser_Start(object sender, EventArgs e)

{

    OnlineUser["User"] = 0; //Initially By Zero(0).

}



//Method For Counting Online Users...


void UserCount_Start(object sender, EventArgs e)

{

    int count;

    count = (int)OnlineUser["User"];

    count = count + 1;

    OnlineUser["User"]=count;

}





protected void Page_Load(object sender, EventArgs e)
{
    Response.Write(OnlineUser["User"].ToString()); 
}
 
Share this answer
 
Comments
Dylan Morley 24-Mar-11 5:06am    
No, that wont work - sessions are specific to a user\client - you need something 'Global' to the application. More along the lines of Pavel Yermalovichs' answer
Micha3ldg 24-Mar-11 9:54am    
Yes this will not work. as Dylan said session is current/per user. instead of using session you can store the count of online in database or you can use Global variable and make it as static. in web static fields are shared among all the users.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900