Question

Usually, when I'm developing websites which interact with clients, I used to use their usernames or user IDs to save their configs and so on. But now I'm developing a website with no login system and I need to save some things, like "likes", "Accessed pages" and more.

I've tried Internet Protocol (IP) but it won't work when the visitors are under NAT or Proxy.

How can I create an unique ID for every single user and them save it in my database?

By the way I'm using PHP.

Était-ce utile?

La solution

This sounds like a recipe for disaster if you are trying to save user data long term.

I would recommend:

  1. Create a random but unique hash and save that to a database.
  2. Set the hash as a cookie on the user's computer.
  3. Save all 'likes', 'accessed pages', et al. to the database keyed on the hash.

The problem arises when the user clears their browsing history/cookies and now there is no way to retrieve that user's activity because it's all based on that hash.

I guess one work-around would be to send an email to the user with their hash and instruct them on how to reinitialize their activity history.

You would have to create a special page where they could enter their hash and your system would reset the cookies on their system.

Autres conseils

You should use combo of cookies, ip and session.

do { 
$uniqueID = uniqid(); 
$exists = mysql_num_rows('SELECT * FROM table WHERE id = $uniqueID');
} while ($exists == 1)

I'm using a similar code to generate unique IDs, and it works fine for me. Or, to make it even simpler, you could just use the uniqid() function, which shouldn't ever really generate two of the same results.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top