1

I have a large amount of traffic and click on an ad at the same time. I need to create a random value to make the value of those clicks and not be duplicated. I tried many random ways but there was still a duplicate problem.

PHP 5

$random=bin2hex(mt_rand());
$clickid=md5($random);

I want clickid is not duplicate when large amount of traffic and click on an ad at the same time.

user3783243
  • 5,368
  • 5
  • 22
  • 41
Mot Dao
  • 13
  • 2
  • Are you looking for https://www.php.net/manual/en/function.random-int.php or https://www.php.net/manual/en/function.uniqid.php – Dharman Aug 11 '19 at 17:50
  • Possible duplicate of [PHP: How to generate a random, unique, alphanumeric string?](https://stackoverflow.com/questions/1846202/php-how-to-generate-a-random-unique-alphanumeric-string) – weegee Aug 11 '19 at 20:23

2 Answers2

0

How about just concatenate multiple mt_rand()?

$id = mt_rand().mt_rand().mt_rand().mt_rand().mt_rand().mt_rand().mt_rand();

If you need a minimum length unique id, you need to save the previous id in some database and get the increased counter in the transaction.

James Bond
  • 2,229
  • 1
  • 15
  • 26
  • thank you so much. i am using this code and i think it is working fine. $rand1=mt_rand().mt_rand().mt_rand(); $rand2 = microtime(); $idclick=md5($rand1.$rand2); – Mot Dao Aug 11 '19 at 18:19
0

Use microtime as the value.
Unless you have sick amounts of traffic then it should be fine.
Microtime is a float value of the Unix time and the microseconds.
That means unless you have thousands of page requests each seconds it should give each one a unique number.

Usage:

$clickid = microtime();
Andreas
  • 23,610
  • 6
  • 30
  • 62
  • thank you so much. i am using this code and i think it is working fine. $rand1=mt_rand().mt_rand().mt_rand(); $rand2 = microtime(); $idclick=md5($rand1.$rand2); – Mot Dao Aug 11 '19 at 18:20
  • Why do you insist on the md5? – Andreas Aug 12 '19 at 03:23
  • Andreas, i want clickid have both numbers and letters. – Mot Dao Aug 12 '19 at 06:15
  • But MD5 means you can't be sure it's unique. MD5 can produce the same output with two different inputs. – Andreas Aug 12 '19 at 06:17
  • Andreas, thank you. Because i don't understand about php. I need unique and both letter and number and about 15-20 characters.(exp : f967861763f8c44). Can you help me a code to creat it? Thanks in advance. – Mot Dao Aug 13 '19 at 00:43