S
S
Seva2012-05-16 22:55:19
PHP
Seva, 2012-05-16 22:55:19

Where to store user IPs?

It is necessary to implement a vote for each material on the site (like / dislike, a la thumbs up and down on youtube). I implemented it as follows: in the table where the articles are actually stored, I added two fields, like and dislike. By clicking on the desired div, an Ajax request is sent to a PHP handler that opens the database, checks how many likes (dislikes) are already there, adds one more, writes down what happened and returns what happened to the calling script.

Now it remains only to figure out how to prohibit voting twice. There is an idea to write down IPs, and when loading the page, check whether this one voted or not, and if so, call the desired div in a different way so that the button is inactive.

Question - where to store IPs? It will not work in the database, because for each article there should be a separate list

Answer the question

In order to leave comments, you need to log in

10 answer(s)
M
max_rip, 2012-05-17
@Zewkin

If cheating is not critical for you, then the best option is cookies / local storage, etc., in general, take it to the client.
If it's critical, then make a simple table with two fields ip (any other identifier) ​​and id news. Make a unique index from these two fields. With a simple join, when you receive data about the news, you will find out whether this client has already voted or not, and the index will save you from re-voting without reloading, and you do not need to check it yourself.
And there is no need for the PHP handler to check what is in the database when you like it, let the database itself do it. Just send an increment or a decriment, and in return ask what happened there.
Well, at the end of a certain period, likes can be turned off, and the sign can be cleaned. You can also get rid of storing numbers in the news itself, but simply count the amounts according to the log table +), but it all depends on your amount of data.

E
egorinsk, 2012-05-16
@egorinsk

There can be dozens on 1 IP. hundreds, thousands of users. For example, Google has a special algorithm for determining the number of users per IP. You can google the PDF.
But in your case, this is unnecessarily complicated - the restriction on 1 vote with IP will suffice.
Store - make a table like user_ip INTEGER(...), article_id, vote. Such a table, if the indexes are correctly placed, works quickly and is easily cached if necessary.

P
Pavel Zagrebelin, 2012-05-17
@Zagrebelion

I hope the phrase "php handler that opens the database, checks how many likes (dislikes) are already there, adds another one, writes what happened and returns what happened to the calling script " does not mean something like this (pseudocode)

counter = sql("select like_count from table where ...");
counter += 1
sql("update table set like_count="+counter+"where ...");
return counter;

V
Vyacheslav Golovanov, 2012-05-16
@SLY_G

Well, databases are there to store data.
You just need to create a separate plate, as advised earlier.
And how to distinguish users - this task is as old as the Internet.
You can set cookies. And in addition to cookies, prohibit voting for ip-addresses that voted quite recently (half an hour, an hour, half a day - it's up to you).
That is, a table with a list of ip and voting time.

I
Ivan, 2012-05-17
@IvanTheCrazy

Now it is fashionable to do all sorts of integrations with social networks. services.
Why not do a social media voting like thebattleofbrands.com/ru ?
And in the database to store the sum of the counters for each of the social networks.

D
denver, 2012-05-16
@denver

If you have anonymous voters, don't defend yourself too much. But if you really need it, then it’s just right to write to the database, because the main question is not where to store it, but how to get it faster, and only key-value is better here (mongodb for example). However, it is unlikely that you will store these logs for months. Subsequently, you will stop voting in a month, so the base will go.
But waiting for a response from AJAX to recalculate the rating on the page is strange. JS has plus and division ;)

M
Maccimo, 2012-05-16
@Maccimo

May vote only to registered users?

K
Konstantin, 2012-05-16
@Norraxx

Have you tried joins?
Try to make a sign for yourself: id + ip + user_id, and then prohibit voting for the second time for each user, prohibiting people from voting without user_id up to a certain number and keep votes for the session.

L
leshka, 2012-05-17
@leshka

tables by tables, but we must not forget about the time when they voted. This will be useful in order not to litter the entire table with millions of records. For example, you can delete IPs that voted more than two weeks ago

A
Anton Bobrik, 2012-05-17
@Firz

Есть идея записывать айпишники, и при загрузке страницы проверять, голосовал такой или нет, и если да — обзывать нужный div по-другому, чтобы кнопка была неактивна.
It is better to implement the “voted or not” check itself at the time of trying to add a new vote, otherwise you will have a stupid load on the database every time the page is loaded + as a result there will be cheating, because. "Not pressed button" does not help at all.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question