开发者

I am developing a CMS for a website that is already existing. How do I count the number of hits on the website? Please advise

I am developing a CMS for an already existing website and I wish to add a counter to count the number of hits on the website. Below is my code. But this increases the counter everytime I refresh the page even if the website is not open at all Could you please advise me the error in my code?

 namespace HGS.HGSAdmin
{
public开发者_开发知识库 partial class FrmEnquiry : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int nCount = 99;
        nCount = GetCounterValue();
        DrawCounterValue(nCount);
    }

    private int GetCounterValue()
    {
        StreamReader ctrFile;
        FileStream ctrFileW;
        StreamWriter sw;

        string strPath = Server.MapPath("myappcounter.txt");
        string strCounterContents;
        int nCounter;
        if (File.Exists(strPath))
        {
            ctrFile = File.OpenText(strPath);
            strCounterContents = ctrFile.ReadLine().ToString();
            ctrFile.Close();
            nCounter = Convert.ToInt32(strCounterContents);
        }
        else

            nCounter = 0;
            nCounter++;
            ctrFileW = new FileStream(strPath, FileMode.OpenOrCreate, FileAccess.Write);
            sw = new StreamWriter(ctrFileW);
            sw.WriteLine(Convert.ToInt32(nCounter));
            sw.Close();
            ctrFileW.Close();


            return nCounter;

    }

    private void DrawCounterValue(int nCounter)
    {
        Response.Expires = 0;
        string strText = nCounter.ToString();
        string strFont = "Arial";
        int nSize = 10;
        Bitmap bmp = null;
        Graphics g = null;

        try
        {
            Font font = new Font(strFont, nSize);
            bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb);
            g = Graphics.FromImage(bmp);
            SizeF oSize = g.MeasureString(strText, font);
            int nWidth = (int)oSize.Width;
            int nHeight = (int)oSize.Height;

            Color clrFG = Color.FromName("Yellow");
            g.Dispose();
            bmp.Dispose();


            bmp = new Bitmap(nWidth, nHeight, PixelFormat.Format32bppArgb);
            g = Graphics.FromImage(bmp);
            g.DrawString(strText, font, new SolidBrush(clrFG), 0, 0);

            MemoryStream oStream = new MemoryStream();
            bmp.Save(oStream, ImageFormat.Gif);

            Response.ClearContent();
            Response.ContentType = "image/gif";
            Response.BinaryWrite(oStream.ToArray());

            Response.End();

        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
            if(null!= g) g.Dispose();
            if(null!= bmp) bmp.Dispose();
        }

        }




        }
    }


There's an approach consisting in writing some cookie - if cookies are enabled in user's Web browser - or with some server session value, so if cookie or session doesn't expire, you can check if user has previously visited some page in your Web site.

This has a counterpart: if cookies are erased and/or expires, and/or server session is abandoned, closed or expires, some user will hit same page more than once a day.

But since there's no definitive way of identifying a machine because you can't log a range of IPs and you can't know user's machine MAC, there're no other approaches that would be effective in this area.

How to achieve that? I believe that you should do that in some in-process or database storage having a hash table or dictionary of page URL and a list there of cookie/session identifiers. This should allow you to check if for some URL, current requester has already visited it.

Something like this (pseudocode of some sort of dictionary):

"/store/product/Duke-Nukem-Forever.aspx" => { "ejojejeo39u3u39", "dhjodhodhodh", "383883dhjd3038" }

Then, in C#:

if(!someSortOfDictionary.ContainsKey(HttpContext.Current.Request.Url))
{
      someSortOfDictionary.Add(HttpContext.Current.Request.Url, new List<string>());
}

if(someSortOfDictionary[HttpContext.Current.Request.Url].Count(id => id == someCookieOrSessionId) > 0)
{
     // Increase hit count!
}

Another advise is you should avoid working with a file resource as counter storage. What would happen if more than an Web request needs to write into this file? I believe you should increase this counter in some database, which is multi-user enabled.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜