# Comparing ip adres with database list

**URL:** <https://www.ruby-forum.com/t/comparing-ip-adres-with-database-list/110342>\
**Category:** Rails\
**Created:** [July 25, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/comparing-ip-adres-with-database-list/110342 "2007-07-25T15:38:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [July 25, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/comparing-ip-adres-with-database-list/110342/1 "2007-07-25T15:38:35Z")

</div>

I’am creating a pollsystem for my site.  
so on each vote in the database the ip adress of the person that vote is  
logged.  
But how can i make that when somebody vote this ip adress not voted  
before.

So how can i compare the list of ip adresses with the new one

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [July 25, 2007, 4:27pm UTC](https://www.ruby-forum.com/t/comparing-ip-adres-with-database-list/110342/2 "2007-07-25T16:27:51Z")

</div>

I am unsure what you mean. But what I would do is drop a cookie with a  
(semi) unique number and store that number in a database. Unless it is  
_very_ important users can’t vote more than once. The problem with  
logging IP’s is that there could be more than one PC behind a single  
IP. And this is quite often the case.

If a user comes to your site you simple retrieve the cookie with the  
number and see if the number already exists in the DB, if so the  
person already voted, if not he did not.

On Jul 25, 3:38 pm, GA Gorter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [July 25, 2007, 4:28pm UTC](https://www.ruby-forum.com/t/comparing-ip-adres-with-database-list/110342/3 "2007-07-25T16:28:55Z")

</div>

On 25 Jul 2007, at 15:38, GA Gorter wrote:

> I’am creating a pollsystem for my site.  
> so on each vote in the database the ip adress of the person that  
> vote is  
> logged.  
> But how can i make that when somebody vote this ip adress not voted  
> before.
> 
> So how can i compare the list of ip adresses with the new one

How about something like (code can be made shorter, but I’ll keep it  
nice and easy to understand):

if Vote.find(:first, :conditions =\> [“ip\_address = ? and poll\_id  
= ?”, request.remote\_ip, params[:poll\_id]])  
flash[:notice] = “You’ve voted for this poll already”  
# Do a redirect, reder or render something using AJAX  
else  
new\_vote = Vote.new(params[:vote])  
new\_vote.ip\_address = request.remote\_ip  
assigned\_poll = Poll.find(params[:poll\_id])  
assigned\_poll \<\< new\_vote if assigned\_poll && new\_vote.valid?  
# Do a redirect, render or render something using AJAX  
end

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [July 25, 2007, 4:36pm UTC](https://www.ruby-forum.com/t/comparing-ip-adres-with-database-list/110342/4 "2007-07-25T16:36:48Z")

</div>

On 25 Jul 2007, at 16:26, harm wrote:

> I am unsure what you mean. But what I would do is drop a cookie with a  
> (semi) unique number and store that number in a database. Unless it is  
> _very_ important users can’t vote more than once. The problem with  
> logging IP’s is that there could be more than one PC behind a single  
> IP. And this is quite often the case.
> 
> If a user comes to your site you simple retrieve the cookie with the  
> number and see if the number already exists in the DB, if so the  
> person already voted, if not he did not.

I would combine the two and even store the vote in a session variable  
for extra security if users have to log in to vote (it all depends on  
how important the unique votes are). Simply said: internet voting can  
always be tampered with:

- People can change their IP (most ISPs use DHCP, so resetting the  
modem gives your user a new IP)
- People can delete cookies
- People can quit their browser, thus resetting the session

The more security you put in place, the more you’re disencouraging  
your user search for a way to cast additional votes.

Best regards

Peter De Berdt

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [July 27, 2007, 9:39am UTC](https://www.ruby-forum.com/t/comparing-ip-adres-with-database-list/110342/5 "2007-07-27T09:39:02Z")

</div>

Thanks for the code Harm my site is working very well now.

@peter That’s true it’s very simple to have another ip adres my neigbors  
have an unprotected wireless network. But this was a project just for  
fun. Now i have a pollcode i think i will make it better and intergrate  
in some of my other projects.
