This code snippet helps you track the number of users visited your Website. This code sample stores a counter in an Application variable and every time a new visitor comes to the page, the counter is incremented.
In Global.asax file, write the following code as specified below:
int userCount; void Application_Start(object sender, EventArgs e) { userCount = 0; Application["mycount"] = count; }
void Session_Start(object sender, EventArgs e) { count = Convert.ToInt32(Application["mycount"]); Application["mycount"] = count + 1; }
Now, in the Page_Load event, use the Application object to display the value whereever required.
protected void Page_Load(object sender, EventArgs e) { if ( !IsPostBack ) { Response.Write("No. of Users viewed the Website: " + Convert.ToString(Application["mycount"])); } }
Please let me know if you have any questions.
Enjoy!!!
-Vijay Kumar Naidu.
|
| Author: roopesh 14 Sep 2007 | Member Level: Silver Points : 0 |
the code for tracking vistors is good.& simple too..
|
| Author: Ravindra Singh 01 Feb 2008 | Member Level: Gold Points : 0 |
code is good
|
| Author: R O B I N 06 Jun 2008 | Member Level: Gold Points : 0 |
Good code for the developer,easy and simple
|
| Author: komaladevi 06 Jun 2008 | Member Level: Gold Points : 0 |
same code is not working for me
|
| Author: sushil kumar 06 Jun 2008 | Member Level: Bronze Points : 1 |
yar code nahi run kar raha hai pl give me ur personal id .. i hv need of this code urgently or mail me at lohsushil@gmail.com sushil
|
| Author: jeeth 08 Apr 2009 | Member Level: Gold Points : 2 |
Due to several reasons application ends in .net(Application_End event also fired), like any change in the root directories or site idle for long time etc. In that case application starts when next request comes and the count variable that you have used will be re-set to zero again. So the code will not work in that case. You need to save the access count to file system or database during Application_End and retrieve it when while Application starts
|