2016-07-27 6 views
-1

데이터베이스에서 검색 한 여러 기사에 대해 위/아래 투표 시스템을 구축하고 싶지만 각 기사에 쿠키를 추가하여 투표 수가 제한되므로 하루 만에 만료됩니다. 적절한 코드를 어디에 추가해야할지 모르겠다.위/아래 투표 시스템에서 쿠키 사용

자세한 내용 :

<script> 
    function vote(id, value) { // function vote with 2 arguments: article ID and value (+1 or -1) depending if you clicked on the arrow up or down 
     var dataFields = { 'id': id, 'value': value }; // We pass the 2 arguments 
     $.ajax({ // Ajax 
      type: "POST", 
      dataType: "text",//This for indicate that you'r expecting a text response from server 
      url: "WebService.asmx/updateVotes", 
      data: dataFields, 
      timeout: 3000, 

      success: function (dataBack) { 
       if(
        $('#number' + id).html(dataBack);// div "number" with the new number 
        $('#arrow_up' + id).html('<div class="arrow_up_voted"></div>'); // We replace the clickable "arrow up" by the not clickable one 
        $('#arrow_down' + id).html('<div class="arrow_down_voted"></div>'); // We replace the clickable "arrow down" by the not clickable one 
        $('#message' + id).html('<div id="alertFadeOut' + id + '" style="color: green">Thank you for voting</div>'); // Diplay message with a fadeout 
        $('#alertFadeOut' + id).fadeOut(1100, function() { 
         $('#alertFadeOut' + id).text(''); 
        }); 
       }, 


      error: function() { 
       $('#number' + id).text('Problem!'); 
      } 
     }); 
    } 


</script> 

위의 코드는 위쪽 화살표 하나마다 사용자가 클릭 당 투표 수를 증가하고 반대로 감소하는 스크립트 호출 아약스 방법이다.

public string updateVotes(string id,int value) 
{ 

    System.Threading.Thread.Sleep(500); // delay for 2.5 seconds Network latency 

    post p = db.posts.Find(int.Parse(id)); 
    // assign new values 
    p.totalVotes += value; 
    db.Entry(p).State = System.Data.EntityState.Modified; 
    db.SaveChanges(); 

    string dataBack =p.totalVotes.ToString(); 
    return dataBack; 
} 

이것은 웹 메소드입니다.

이제 큰 소리로 생각해 보았습니다. 쿠키가 null인지 아닌지에 관계없이 다음 함수를 ewxamine에 코딩합니다.

public bool enableVoting() 
{ 

    HttpCookie cookie = Request.Cookies["enableVote"]; 

    if (Request.Cookies["enableVote"] != null) 
    { 
     return true; 

    } 

    else 
    { 

     return false; 
    } 


} 

나는 잘못 알고 있지만 적어도 시도했습니다.

또한 어디에 사용자가 기사에 투표 할 때마다 쿠키를 추가 할 각 루프를 추가 할 것인가?

foreach(post p in db.posts){ 
     HttpCookie cookie = new HttpCookie("enableVote"+p.ID); 
     cookie.Value = "article:"+p.ID; 
     cookie.Expires = DateTime.Now.AddDays(1); 
     Response.Cookies.Add(cookie); 
    } 

답변

0

제안. 쿠키를 사용하지 마십시오. 그들은 쉽게 조작됩니다. 브라우저 기록을 지우고 다시 투표 할 수 있습니다. 다시 한번 .. 또 다시 ..

대신 데이터베이스에 투표 테이블을 만들고 유권자의 ip와 투표 한 게시물의 ID가있는 레코드를 타임 스탬프와 함께 추가하십시오.

이 방법을 사용하면 투표 수를 쉽게 계산할 수 있으며 누군가 투표를하면 IP가 해당 기사 (또는 모든 기사)에 대해 마지막으로 투표 한 지 얼마되지 않았는지 신속하게 확인합니다.

또한 데이터베이스의 투표 테이블을 사용하면 모든 항목을 위 또는 아래로 봇하는 봇을 쉽게 잡을 수 있으며 단일 IP가 모든 기사에서 작성할 수있는 득표 수를 제한 할 수 있습니다 (몇 분마다 1 ~ 2 표 이상 허용).).

같은 IP 뒤에있는 여러 사람이 걱정할 경우 브라우저 이름을 포함시켜 고유 IP 및 브라우저 버전 만 투표로 계산할 수 있습니다. 이것은 상당히 독특합니다. 또한 조작 할 수 있지만 일반 사용자에게는 조금 더 어렵습니다.

업데이트 : 이 코드는 내 MVC 프로젝트 중 하나에서 다소 고유 한 키를 얻기 위해 사용합니다. 사용자는 브라우저를 전환하고 다시 투표 할 수 있지만 브라우저 기록을 삭제하는 것보다 많은 노력과 많은 고통을 겪습니다.

IP, 브라우저 및 국가를 문자열로 결합하여이를 투표 키로 사용합니다.

public class UserInfo 
{ 
    public String ip { get; private set; } 
    public String browser { get; private set; } 
    public String country { get; private set; } 

    public UserInfo(HttpRequestBase Request) 
    { 
     ip = Request.UserHostAddress; 
     browser = Request.Browser.Platform + " " + Request.Browser.Type + "/" + Request.Browser.Id + " " + Request.Browser.Version; 
     country = ""; 

     if (Request.UserLanguages.Length > 0) 
      country += " - " + Request.UserLanguages.ElementAt(0); 
    } 
} 

내가 여기에이 시스템을 사용하고 있습니다 : 사용자가 이전에 업로드 된 파일이있는 경우 http://filepublicator.com/ 확인하기 위해 (이 목록에있을 것입니다, 뭔가 가까이 브라우저를 업로드하고 다시 거기 가서보십시오).

+0

조언 해 주셔서 감사합니다. 유권자의 신분을 얻는 방법을 설명하기위한 예제 또는 링크가 있습니까? –

+1

Asp.net C#을 사용하면 그렇게 할 수 있습니까? –