I am learning and trying to create a site using PHP.
How can I create a unique like button. A user should be able to like a content only once
I could create a user login and save the information in the user database but I would like that if unregistered users could also like the contents.
You need some way of detecting unique users. One way is to use the IP address, but note that it is not perfect because more than one person can use the same IP and sometimes their IP changes.
I was also thinking about storing the IP in a database.
How can cookies be used? Will it be more practicable?
This is for logged in users right. I think you should start with the database. Like we have a users table and a sites table joined by a relational table called likes
I am trying to make the like button accessible for the non-users as well.
Without them signing in, they could like a content
So what could be used to identify a particular user?
This will not prevent the usage of proxies. Many web proxies use the same User-Agent header as the original user so I guess you could try catch if the exact same User-Agent is being used from many IPs in a short time. I guess there are more header fields you could use to make it more reliable but not sure if it's worth the trouble anyway.
Even with a sign-up system people can abuse the system by creating multiple accounts.
If you want you could let each like only count for a fixed amount of time so if no one likes the item it will eventually drop down to zero. This would make abusing less attractive because it will have to be repeated once in a while but this way is probably only suitable for pages with many visitors otherwise most of the items will always have very low like count.
cookies can be removed and will not be there when the same person uses another browser, same goes for sessions.
I'd say, only registered users can like something...
besides, why would you want to have unregistered users "like" something.
this would result into "userX, userY userZ and 142 unregistered users like this"...
Yeah you've got a point
I should make it accessible for the registered users only.