Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create a unique id for every new visitor?

my question is how do create a unique id (which will go at the end of the URL, e.g. http://example.com/?id=12345) for every unique visitor who visits my site. I am also wondering how to track the visitors ip address, so that they only receive one id/link. In other words, how to make a script which generates one unique id per IP address. In short, I want a script which generates a unique ID for every new visitor. So for example: visitor 1's id=http://example.com/?id=12345, visitor 2's id=http://example.com/?id=77369, and so on. Thanks.

like image 717
user719813 Avatar asked Sep 06 '25 10:09

user719813


2 Answers

Easiest thing might just be to use session_start and session_id.

 session_start();
 $id = session_id();

You can then save off $id and associate it with whatever you want in a database or whatever.

Alternative, just save off a very accurate timestamp in microseconds

$id = microtime();

Or create a row in a database per user and use the primary key of the row.

like image 108
Doug T. Avatar answered Sep 09 '25 03:09

Doug T.


All of the answers can be incorporated simply enough. However, something you may not have thought about is what happens when more than one user shares an IP. For example, all of the employees at Google or all of the users at AOL or Juno may use 100 ips among all of them. What if they are served new IPs every 10 - 25 days.

The idea behind data is that is is meaningful. How will your data be meaningful? The IP could be a 12 year old kid from upstate New York one day and a 40 year old women from Hackensack New Jersey the next. What good does it do you to implement such a system besides complicating your code?

My Advice: Revisit your requirements. What are you really trying to accomplish? It may be better to implement a simple cookie to track unique visitors. It still won't be 100% accurate (what about the people who visit from internet cafe's all paying to use the same computer?). But it would make the code much less complicated not having to append every url with all of the extra parameters.

In short, the only way to ensure a unique ID per visitor is bay asking them to login. Granted they could potentially set up multiple accounts using different email addresses, but at least you are guaranteed that no two separate people will get the same id.

like image 24
Chuck Burgess Avatar answered Sep 09 '25 02:09

Chuck Burgess