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

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

출처 )  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;
}
}

댓글 남기기

이메일은 공개되지 않습니다.

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.