carthag has asked for the wisdom of the Perl Monks concerning the following question:
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
|
---|
Back to
Seekers of Perl Wisdom