I'm trying to create a hit counter for my website

I have looked at a lot of examples on the net, but most don't have the correct forms I am looking for.

For example, on most hit counters i have created, all i do is press refresh and the hit goes up. I don't want that. I have also tried to use an .xml file, to no avail. I just hold in the F5 key, and my hits go up and up. So I know I need to use a database. I plan on using a MS Access database, as it is small (will only have the one table), and can be easily put on the site when published.

I have a general idea as to how to add a hit:

OleDbConnection conn = null;
OleDbDataReader reader = null;
try
{
    conn = new OleDbConnection(
    "Provider=Microsoft.Jet.OLEDB.4.0; " + 
    "Data Source=" + Server.MapPath("App_Data/TestDB.mdb"));
    conn.Open();

    OleDbCommand cmd = 
    new OleDbCommand("insert into Hits Values(" + hits + "", conn);
    cmd.ExecuteNonQuery();

}
catch (Exception e)
{
    Response.Write(e.Message);
    Response.End();
}

That is how it should be. It is similar to SQL I hope. Anyway, my problem would be, when the user returns to that page (Home), the hit counter increments. I have heard of active sessions, and was wondering if that is what I should use. If that is the right way to go, how do I fully implement it on my site? Basically, how can I ensure that Bob logs on and uses the site, visits all the pages 5 times and the hit counter only registers once? So that when Jane logs onto the site, she sees the she is the second person (after Bob) to view this site?

Will it be something along the lines of:

Session["logged"] = "On";
if(Session["logged"].ToString().Equals("On");
{
    hits = 1;
    //add the data to database

}

From here I am lost. And if the database can be avoided in favor of an .xml file, would the active sessions still work for that? And lastly, do I have to be concerned about cookies?

And please, I know that Google analytics offers a free service, but I don't want to use them. I need to also learn to develop these things on my own.

Thanks

有帮助吗?

解决方案

if the database can be avoided in favor of an .xml file, would the active sessions still work for that? And lastly, do I have to be concerned about cookies?

Use a session variable to track whether or not you have counted the "hit". What you're really wanting to track here is Visits as opposed to "hits" or "pageloads".

In IIS, sessions last for the duration of the clients visit. There are a few gotchas. If the client does not support cookies, every pageload will end up being a different session. You can use a combination of IP and UserAgent to detect when this happens, but that means you'll need to store that in your database and somehow check against it when you're running code to count the "hit".

You don't really need to use cookies to make this work, unless you want to be able to match client sessions to see if the visitor is new or was previously on your site at some point. When I built this I opted to store a unique "cookieid" on the client so that I would be able to detect repeat visitors.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top