0

我正在建立一个在线投票系统。我需要检查是否定义了某个 cookie。如果不是,则增加投票。否则,什么也不做。

我只是使用非常简单的代码。谁能解释我错过了什么?

投票代码:

<cfprocessingdirective suppresswhitespace="yes">
<cfsetting showdebugoutput="no" enablecfoutputonly="yes">
<cfoutput>
<cfset user_post_type = "#trim(form.vote)#">
<cfset unique_content_id = IIf(IsDefined("FORM.unique_id") AND Int(FORM.unique_id), Int(FORM.unique_id), DE("0"))>
<cfset unique_content_id = Hash(unique_content_id)>
<cfswitch expression="#user_post_type#">
<cfcase value="up">
    <cfif isDefined('cookie.votes_#unique_content_id#')>
  <!--- Do Nothing  or show an alert --->    
        <cfelse>
        &#9;<cfquery name="matchVotes" datasource="#application.cw.dsn#" username="#application.cw.dsnUsername#" 
            password="#application.cw.dsnPassword#">
                select vote_up from voting_count where unique_content_id = '#val(form.unique_id)#' limit 1
            </cfquery>
            <cfif matchVotes.recordcount>
            <cfquery name="updmatchVotes" datasource="#application.cw.dsn#" username="#application.cw.dsnUsername#" 
            password="#application.cw.dsnPassword#">
update voting_count set vote_up = vote_up+1 
                where unique_content_id = '#val(form.unique_id)#'
</cfquery>
            <cfelse>
            <cfquery name="insertmatchVotes" datasource="#application.cw.dsn#" username="#application.cw.dsnUsername#" 
            password="#application.cw.dsnPassword#">
insert into voting_count(unique_content_id,vote_up) 
                VALUES('#val(form.unique_id)#',1)
</cfquery>
            </cfif>
            <cfcookie name="cookie.votes_#unique_content_id#" value="1" expires="#createTimeSpan(0,2,0,0)#">
         </cfif>   
         <cfquery name="getVotes" datasource="#application.cw.dsn#" username="#application.cw.dsnUsername#" 
        password="#application.cw.dsnPassword#">
            select vote_up from voting_count where unique_content_id = '#val(form.unique_id)#' limit 1
         </cfquery>   
<cfset total = getVotes.vote_up>
            <cfoutput>#total#</cfoutput>
    </cfcase>
</cfswitch>
</cfoutput>
<cfsetting enablecfoutputonly="no">
</cfprocessingdirective>
4

1 回答 1

1

首先想到的是您正在像这样设置cookie:

<cfcookie name="cookie.votes_#unique_content_id#" 
      expires="#createTimeSpan(0,2,0,0)#"
      value="1">

当你真的应该这样做时:

<cfcookie name="votes_#unique_content_id#" 
      expires="#createTimeSpan(0,2,0,0)#"
      value="1" >

因此 usingCFCookie已经将您的变量添加到 cookie 范围内。

试试看,让我知道它是否有效。

于 2013-05-02T13:45:31.490 回答