쿠키를 이용하여 조회수 올리기

쿠키를 이용하여 조회수 올리기

출처 )  http://www.moros.kr/119

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;
}
}
}

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
{
// 조회수 올림
query = “UPDATE tbl_board SET b_hit = ” + (b_hit + 1) + ” WHERE b_no = ” + b_no;
command = new SqlCommand(query, connection);

connection.Open();
command.ExecuteNonQuery();
connection.Close();

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