I run a little script on my server where people can moderate a link (basically up & downvotes). This is run in a CGI::Application I wrote myself, but I'm having a bit of troubles finding a better way to prevent the users from voting multiple times (I don't need top security or anything, I just don't want easy ballotstuffing).

So far, my code looks like this:

sub link_moderate { my $self = shift; my $q = $self->query(); my $output = ""; my $tmpl = $self->load_tmpl('.tmpl'); my $uri = $q->param('uri')||""; my $direction = $q->param('direction')||""; #FIX!! (check for existence of URL) my $statementHandle = $Database->prepare("SELECT COUNT(*) FROM mod +eration WHERE userkey LIKE '".$q->remote_addr."-$uri';"); my $returnValue = $statementHandle->execute; my @results = $statementHandle->fetchrow_array; if ($results[0] >= 1) { $tmpl->param('message' => "Ahem, you've alredy voted at this. +Stop doing it, you won't get any further."); } else { if ($direction eq "up") { my $returnValueStatus = $Database->do("UPDATE saved_search + SET moderation=moderation+1 WHERE uri LIKE '$uri';"); $returnValueStatus = $Database->do("INSERT INTO moderation + VALUES('".$q->remote_addr."-$uri');"); if ($returnValueStatus == 1) { $tmpl->param('message' => "The moderation value for th +e url has been updated."); } else { $tmpl->param('message' => "The URL could not be found +in the database."); } } elsif ($direction eq "down") { my $returnValueStatus = $Database->do("UPDATE saved_search + SET moderation=moderation-1 WHERE uri LIKE '$uri';"); $returnValueStatus = $Database->do("INSERT INTO moderation + VALUES('".$q->remote_addr."-$uri');"); if ($returnValueStatus == 1) { $tmpl->param('message' => "The moderation value for th +e url has been updated."); } else { $tmpl->param('message' => "The URL could not be found +in the database."); } $Database->do("DELETE FROM saved_search WHERE moderation < += -15;"); } else { $tmpl->param('message' => "Eh, what direction would '$dire +ction' be?"); } } $tmpl->param('version' => $VERSION); $tmpl->param('number_of_links' => &get_number_of_links); $tmpl->param('list_urls' => &getlast(10)); $output = clean_html($tmpl->output); $self->header_props(-Content_length=>length($output)); return $output; }

As you can see, there goes an awful lot of data into the moderation table, and it's generally not very pretty.

Any thoughts on this?

Edit kudra, 2002-10-27 Added a readmore tag


In reply to Preventing multiple votes by same user effeciently by carthag

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.