Log in

View Full Version : Raffle script to reward Voters



Webiter
11-06-2011, 05:46 PM
Running a (local area) poll on a website which records ip address (to a flat file) to prevent multiple votes .

I seek to make arrangements to provide a reward incentive for voting.

Could a prize be given further to a random ip address selection at the end of the poll. The winning ip address could be published on the website thus enabling winner to claim prize.

Question 1. Is such a thing possible! :o
Question 2. Are computer users familiar with their ip addresses?:o
Question 3. How would a person prove that their computer was the winner? :o
Question 4. What software or script would make the random selection from the ip addresses? :o

Thanks in advance.

djr33
11-06-2011, 06:11 PM
There's a much simpler and more reliable option: have them submit their email address when they vote. Then you can contact whoever wins by email. That's reliable and everything already works.


But to answer your questions:

Question 1:
In theory it's possible, but it would not be practical because of a few things. First, IP addresses are not actually unique for each user. If two users share a computer (family members, or even users at an internet cafe or a school), then you won't know which user it was. Second, IP addresses change. They're reliable during a single visit to the website, but they might change every day, or every few weeks, depending on a number of factors (fast for dialup, for example). But more importantly, you would be requiring that users log on from their home computer only, or at least that when they try to claim the prize they'd be on the same computer (which hopefully has not reset its IP, which is unlikely). And mobile devices rotate IPs all the time, so they won't work for this.

Question 2:
No, the average internet user does not know their IP address. You could probably fairly easily explain what it is, and you could even use PHP for example to show them their IP. I'm not sure how much you'd really need to explain, but if there was a question about knowing which computer to use, that could be very confusing.
In short, don't rely on the user understanding this at all. (Some will, so be prepared to know more than them in case they question the fairness of your system.)

Question 3:
Well, if they log on from the same IP, then it's probably the same person. That's not entirely true: if the IP rotates, then it might be someone else. If the computer/connection is shared, it might be someone else. But on average the particular users participating in your poll probably won't be "trading" IPs all that often. You might end up with no winners (since the winning IP isn't being used by anyone who knows about the contest at that point), but the odds are against someone "falsely winning" because they received the winning IP. However, the odds actually are a little higher if you are doing this locally because IPs are grouped by geographic location. It's a little more complicated than that, but within every subset of geography (eg, country, city) the IPs tend to be more similar. This is especially true if many of your participants are using the same ISP.

Question 4:
Any programming language could do this. You could even do it in Microsoft Excel for example. Or you could do it by hand: just find ANY random number generator and locate the IP on the list at that point (eg, if you get "530" as the random number, find line 530 in your file and use that IP). If you're going to do this a lot I'd recommend approaching it with PHP to automate everything, but there's no reason you NEED to, just to save yourself time. And in fact, I'd recommend using a database instead of flat files, but that's your choice.


Some more comments:

1. The faster you do this, the more likely the IPs will not have changed. So if you give out a prize every day to the people that participated that day, the odds are higher that they will still have the same IP address, but it's not guaranteed. If you wait a year (or even a month) there's no point at all: it's almost guaranteed that the IPs have rotated. The only people that have static IPs have that because they want their computer to be located externally, so servers often have static IPs (although not always), but most home computers don't.

2. How would you let the participants know? "Check back in a while to see if your computer lets you claim a prize?" I don't see how using the IP here is easier. Why not just collect emails like I said?

3. A more reliable method if you don't want to use emails would be to give each participant a random number. This is just like a lottery/raffle ticket. Tell them to check back and enter their number to see if they've won.

4. You could ignore these problems and just search for a user whose IP has not changed. So just rotate through a list of potential winners until one of them claims the prize because for some reason their computer is slower to reset its IP than the other computers involved. But this would NOT be fair, because you would be giving out prizes based on who keeps the same IP, not on who is really a random winner. Some participants would never be able to win.

5. Using IPs to identify participants is always a bad idea because it's easy to cheat. Many users might not understand this, but the odds are that someone will understand. If you use multiple computers, there's no way to link the votes, so one user can vote many times. Especially if there is a prize involved, it's almost guaranteed that they will try to enter multiple times and that then you'd have many IPs for a single person.
There are two ways to deal with this:
1) If it isn't a very important poll or something that people would want to cheat on (usually when there isn't a prize), then you can just ignore it. If someone votes twice or three times it won't really matter in the end, as long as you have a high number of participants.
2) Use email validation. Send a code to the email they submit when voting then only count the vote when the code is re-entered into your site. It's more work, but it's reliable. One vote per email.
Or, (3), you could create a user login system (or use on from a forum, etc.) if that happens to be easier. One vote per account.

Note that (2) and (3) don't actually prove that a single person doesn't vote more than once (they might have two emails or two accounts), but it's more difficult, and in the end it will be easier to give them the prize because those will be unique.

Webiter
11-06-2011, 11:54 PM
Thanks for the detailed response and well that sorted that! Your first sentence seems to give the proper opinion and direction on the matter.:)
I am using a DBscript.net PHP Poll script which I like to master the polling. I might be :mad:
Is knitting the email solution into such a script a straightforward or wise option for a one year old webdesigner :o ?

djr33
11-07-2011, 04:23 AM
In theory, adding an email input is just one line of code. Making sure that actually gets sent to you is a little more complicated and depends on the system. If you're using a polling script, then look at the script for existing options. If there aren't any, then you'll either need to use another script or modify that one (maybe in a big way or maybe just very slightly). It really depends on the script. Setting up something else (eg, IP addresses or a 'lottery' code) wouldn't really be much easier, so you probably should just figure out how to add the email input.

Here's the HTML you'd need:
<input type="text" name="email" />
Add that inside the <form> tags for your poll.
And that's all. That will be submitted with the form and received on the server, but unless the script intentionally forwards all submitted fields (it might) or looks for one called "email" specifically, then it probably won't actually be sent to you. But the rest of the modifications will need to take place inside the PHP, not in the HTML. (Unless it is looking for another input name for the email, eg, 'mail' or 'address', etc.)


By the way, if I were you I'd recommend checking for any duplicated email addresses (meaning they voted two times) then removing them from the potential winners.

Webiter
11-07-2011, 01:31 PM
The poll layout is described as show_vote_control


<title>Polling Booth</title>
<?php
require_once('poll/poll.php');
?>

<div class="poll"><h3>Poll 3</h3><?php show_vote_control('1'); ?></div>

It appears that the email input may have to go inside the POLL DEFINATION!
In config.php POLL DEFINATION is described in the following terms:


$p = new Poll;
$VALID_POLLS["1"] = $p; // "1" is the poll ID
$p->legend = "Our First Poll"; // Form legend; leave empty for none
$p->returnToURL = "../booth.php"; // Specify the URL to return to for this poll.
$p->control_type = $CONTROL_RADIOBUTTONS; // Control type; $CONTROL_RADIOBUTTONS or $CONTROL_COMBOBOX
//POLL QUESTION//
$p->question = "Do we need a Town Council?";
//POLL ANSWERS//
$p->add_value("1", "Yes");
$p->add_value("2", "Probably");
$p->add_value("3", "No");
$p->add_value("4", "Undecided");

and this renders as img attached with the information relevent to the voter input being sent to the flat file.

Using HTML to install as follows:
Include your EMAIL to Validate <input type="text" name="email" /> just before the </div> of the <div class="poll"> renders an email input field - how such input might find its way to the flat file along with the vote and the ip detail would resolve!

Not sure if this helps you to add further comment on task direction!

djr33
11-08-2011, 12:03 AM
There's a general correlation between ease of use and difficulty of modification, or of ease of modification and difficulty of use. "Do it yourself" things are easy to modify, but hard to use; and pre-made "ready to go" scripts are easy to use, but hard to modify.

What you have here is a prebuilt script that just might not have the option to add an email field. Or it might, but what you posted isn't "real" code-- it's pseudo-code based on the framework of that script. (It's 'real' PHP code, but not in a way that allows easy modification-- it's all an indirect way for the PHP to eventually generate the real script.)

Now it's possible that there's a way to add the email field, but I do'nt know how that would work from what you've posted. Do you have a link to the script's documentation? We might be able to help that way. If not, you need to change scripts or hire someone to modify thing one for you.

Another option that might work is the following:
After they vote on the poll, ask for an email address to enter them into the contest. Then that is stored independently of the votes. This way, you won't need to integrate it at all. It's not a perfect solution, but might be easiest for you given the circumstances.

Webiter
11-08-2011, 01:13 AM
djr33 said "After they vote on the poll, ask for an email address to enter them into the contest. Then that is stored independently of the votes. This way, you won't need to integrate it at all".

I like this approach as voting would be occasional, lasting about a week and I only seek to encourage by adding a reward facility. (Our committee could get some supermarket to sponsor a food hamper or something like that when required). Requesting the addition of the email to enter the raffle would be a simple and straightforward way out. :) It also gives the added impression that we know what we are doing!!... advice from my old schoolteacher and yes, its better not to tamper with the working poll script.

Could the emails associated with the polling be easily collected to a seperate flat file (do not want to bother with mySql just yet) and where might the email form be sending to?

This is getting close to the principles used in having the detail on a contact form being sent to an email address, except in this case we want to try and get it deposited in a file! You might be better able if possible to sketch the roadmap to such a resolve. :o

djr33
11-08-2011, 05:47 PM
Using MySQL would be better for organizational purposes, such as quickly finding a random email to give the prize to. But you don't really need it-- if you use a flat file you'd just get a list of emails then you could randomly select one yourself (eg, open the list in a program that has line numbers then use a random number generator to find the one you want to pick).

Here's the relevant information. It's not necessarily "easy", but it's pretty basic/standard PHP so you should be able to put it together from a few basic examples.

First, you'll need to make an HTML form. Then add a text input (like I had in my earlier post) and give it a name you'll remember-- 'email' is fine.

Next, on the PHP page you'll need to receive that value:
$email = $_POST['email'];
But (this is a little advanced) you'll want to be sure that:
--1) something WAS submitted
$email = !empty($_POST['email'])?$_POST['email'];
--2) it doesn't contain any 'funny' characters. Actually, if this is JUST a text file, it won't matter. So I'll skip it-- but look into 'escaping' if you need to use this in a database or later display it on an HTML page. You could use htmlspecialchars() to remove <, >, & and ", for example. Or you could use mysql_real_escape_string() to make it safe for a database.

Then you have the variable $email all ready to go-- just insert it into a text file.
http://www.php.net/manual/en/function.fwrite.php
There's documentation there about how to do this and some example scripts.
Some extra info:
1. You will need to make sure PHP has write permissions on the file. The easiest way to do this is to create a new file (via FTP, for example) and set its permissions specifically to 755 or 777 (CHMOD). Look up 'chmod' if you don't know how to do this. It's a little confusing, but not difficult.
2. You will want to look at the writing mode. You could read the contents of the text file, then add the new email to the end, then write all of that back to the text file. But the simpler way would be to use one of the writing modes that just adds the contents to the end: each time you use fwrite() it just adds that next email as a new line in the file.
3. To specify a new line, you'll need to use "\n" (in DOUBLE quotes-- not single quotes), or you could just add a real new line in the string. Some examples:

$email = $email."\n"; //add a line break after.
$email = $email.'
'; //add a line break after

Webiter
11-08-2011, 09:29 PM
My index.php consists of straightforward inputs. Now added name and phone.

Processing as follows in raffleinfo.php


<?php
$vname = $_GET["name"];
$vemail = $_GET["email"];
$vphone = $_GET["phone"];
print("<b>Thank You!</b><br />Your information will be entered once into the raffle!");
$out = fopen("savedinfo.php", "a"); //what is this "a" doing?
if (!$out) {
print("Could not add details to the database");
exit;
}
fputs ($out,"\n"); //add a line break after.
fwrite($out,"$vname, $vemail, $vphone.");
fclose($out);
?>

Currently running the files in wamp and collected info being captured in savedinfo.php (or the flatfile) as required.

Thanks Daniel for your efforts and guidance with this query. You pulled me off the wrong track ane positioned me on the right track. Cheers.

Webiter
11-11-2011, 10:39 PM
I present this just to present the question at the end of this post.

Presented alongside a polling facility on a webpage is the following input fields that enables the visitor (voter) to enter their details in a raffle for a hamper as a reward for their effort in taking part in the poll.

The raffle entry form script is not attached to the poll script. All inputs in the raffle entry form script are validated and the info is sent to a flatfile. It is a short poll lasting only 7 days on different topics that might arise from time to time for a small town forum. (ip is also collected)


Name :<input type="text" name="visitor" /><br /><br />
Email :<input type="text" name="visitormail" /><br /><br />
Phone :<input type="text" name="visitorphone"/><br /><br />

On submission the 'Thank You' page advises the visitor that their details will be used once in the raffle for the hamper.

In other words the visitor could go back and fill in the form again and submit, requiring that I check for and weed out multiple or repeat entries from the flatfile before completing the Random Number draw for the hamper! Such a task would not be the end of the world but it might be tedious.

The Question Is there a simple way based on the info collected to install something that in some way prevents the visitor from attempting multiple entries into the draw? It would not have to be 100% but it might save me some time on a tedious activity!

djr33
11-12-2011, 03:45 AM
There isn't anything too easy to do that, but you could filter the email list for duplicates then select from the others. That would be done manually, but it's certainly possible. The easiest way would be to select an email address as the winner. Then remove it from the list. Then search the list for that same email (edit>find, or the equivalent). If you find it, start over and look for a new non-duplicated email. If you don't find it, then that person entered just once.

It's also possible to read the file using PHP, split at each line, then actually sort through them-- remove duplicates, then pick a random one, all using PHP. But unless you'll be doing it often, it'll probably be faster for you to just do it yourself rather than try to code that.

Webiter
11-12-2011, 09:13 AM
Thanks djr33, I did not think that it would be easy but I was curious to ask the question. Thanks for the clarification.

djr33
11-13-2011, 04:06 AM
By the way, if you're using a database (or know how to deal with searching for information in text files in PHP), then you could do this automatically. But based on the way you're approaching it, it would be easier to look through yourself -- I just wanted to add that it is completely possible if you design the website another way.

Webiter
11-14-2011, 12:09 PM
Yes, I will be using a manual approach as I am only expecting 500 to 1000 entries. I am sure a short period of my time with Edit>Find would sanitize the file of any multiple inputs. :)

I thought that it was however good to have gone through this with a flatfile approach as it would be easy to replicate again and again for a small job running for a short period. I might be expressing a primitive view here - but the learning curve was not to steep! :o You might have a different opinion on that!

Onwards and upwards, I will soon be wrestling with SQLite and who knows my opinion on flatfiles might then change. :)

By the way I found this great Demo on using Flatfiles as a database at: http://www.designdetector.com/archives/04/10/FlatFileDatabaseDemo.php#demo