Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
How to store cookies in PHP to uniquely identify user voting without login

Context

I have a debate system on my website where users can click on the option of the debate they want to vote for, which then reveals the tally of the debate votes on either option. However, users need to log into their account for this to work, so users who are just passing by are unable to vote (you can check the functionality here). I'm looking to alter this code so that users can vote on debates without logging into their account. Knowing that most options are not completely reliable and can be worked around, I decided to settle on using cookies to store information that would identify if the users have voted on the debate or not without being logged in.

Question

As I am new to working with cookies, I was wondering: What is the best way to implement cookies so that I can insert the debate votes into my database and check against it when the non-logged-in user clicks on the same debate twice? I figured that I would just need a unique number that would be stored as a cookie on the client side until the cookies are removed. With this in mind, I thought I could just create a random number based on the user IP address, like so:

setcookie('temp_id', md5($_SERVER['REMOTE_ADDR']);

I know that users can change the IP address, but I didn't think this would matter as long as there's a number stored on the client side. Is this a good way to obtain a unique number that can be stored as a cookie on the client side, or should I be setting a different unique number that would work better for some reason? Is this a safe thing to do? There doesn't seem to be any important private user information in there.

The next part of the problem is how I would alter my database code to accomodate those who are not logged in. My database table poll_votes is currently configured like so:

CREATE TABLE poll_votes (
    vote_id INT(11) AUTO_INCREMENT PRIMARY KEY NOT NULL,
    yes VARCHAR(50) NOT NULL,
    no VARCHAR(50) NOT NULL,
    poll_id INT(11) NOT NULL,
    idUsers INT(11) NOT NULL,
    article_id INT(11) UNSIGNED,
    seen TINYTEXT NOT NULL,
    FOREIGN KEY (article_id) REFERENCES articles (article_id) ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY (poll_id) REFERENCES polls (poll_id) ON DELETE CASCADE ON UPDATE CASCADE,
    FOREIGN KEY (idUsers) REFERENCES users (idUsers) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;

As you can see, one of the colums in my table is idUsers, which references the registered users in the table users. This means that I will have to alter the table by adding another column that would keep track of the unique cookie ID when the user votes on a debate and also set idUsers to NULL, correct? I wanted to get a check on this before I implemented it to be sure I wasn't misunderstanding how cookies could be used in this situation to solve this problem. Let me know if there's any confusion. Thanks.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I see a couple of issues. We built a couple of voting systems for music events and you usually get a lot of users without accounts.

First of all IP-address is not a good idea.

setcookie('temp_id', md5($_SERVER['REMOTE_ADDR']);

Let's say I'm at shared office of DigitalEconomyHub.com - there are multiple coworkers and we all have the same IP-address. I would suggest to use UUID - almost endless unique number. In other way my coworkers would see what I voted for (not cool).

setcookie('voter_uuid', uniqid($_SERVER['REMOTE_ADDR'], true));

Secondly, the DB expects you to have a user object all the time.

idUsers INT(11) NOT NULL

User objects for the random visitor? Will generate too much junk records. Lets do it differently - idUsers becomes a nullable field and we add a field voter_uuid that is varchar and indexed.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!