출처 : http://www.devpia.com/MAEUL/Contents/Detail.aspx?BoardID=13&MAEULNO=6&no=285&page=1

private string _sCookieId = string.Empty;
    private string sCookieId
    {
        get{return _sCookieId;}
        set
        {            //사용자 정보 얻기
            if (Page.User.Identity.IsAuthenticated == true)
            {
                _sCookieId = Page.User.Identity.Name + "/" + value;
            }
            else
            {
                string sRemoteIP = (string)HttpContext.Current.Request.UserHostAddress;
                _sCookieId = sRemoteIP + "/" + value;
            }
        }
    }

    // 쿠키를 이용하여 조회수를 1증가 시키는 메서드.
    private void IncreaseReadCountCookie(string TableName, string boardid)
    {
        bool dupliChk = false;
        string c_read_idx = string.Empty;
       
        sCookieId = boardid;

        if (Request.Cookies[TableName] != null)
            c_read_idx = Request.Cookies[TableName]["READ"];   
        else
            c_read_idx = string.Empty;
       
        Response.Cookies[TableName].Value = TableName;
        Response.Cookies[TableName].Expires = DateTime.Now.AddDays(1);

        string [] arrRead = c_read_idx.Split(',');
        for (int i = 0; i < arrRead.Length; i++)
        {
            if (sCookieId == arrRead[i].ToString().Trim()) dupliChk = true;
        }

        if (dupliChk == true)
        {
            Response.Cookies[TableName]["READ"] = c_read_idx;
        }
        else
        {
            //// 조회수 1 증가.
            SqlCommand cmd = new SqlCommand("board_UPDATE_BOARDREADCOUNT", DbConn.GetConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@boardid", boardid);
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();
            cmd.Connection.Close();

            Response.Cookies[TableName]["READ"] = c_read_idx + "," + sCookieId;
        }
    }

    // 세션을 이용하여 조회수를 1증가 시키는 메서드.
    private void IncreaseReadCountSession(string TableName, string boardid)
    {
        sCookieId = boardid;
       
        //해당 게시물의 ID와 IP의 조합으로 세션이 생성되었는지 따진다.
        if (Session[sCookieId] == null)
        {
            SqlCommand cmd = new SqlCommand("board_UPDATE_BOARDREADCOUNT", DbConn.GetConn());
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@boardid", boardid);
            cmd.Connection.Open();
            cmd.ExecuteNonQuery();

             //중복방지를 위한 세션 생성 Session[IP/BID]
            Session[sCookieId] = "1";
        }
    }

+ Recent posts