Gurpreet Singh
Gurpreet Singh

Reputation: 23

Issue with Voting System, Cookie Issue

I am building an online voting system. I need to check if a certain cookie is defined. If it is not, then increase the vote. Otherwise, do nothing.

I am just using very simple code. Can anyone explain what I am missing?

Code for Voting:

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

Upvotes: 0

Views: 124

Answers (1)

Marcos Placona
Marcos Placona

Reputation: 21730

First thing that comes to mind is the fact that you're setting your cookie like this:

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

When you should really be doing it like this:

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

So using CFCookie already adds your variable to the cookie scope.

Try that, and let me know if it works.

Upvotes: 1

Related Questions