-2

(This question is not about PHP type-casting.)

I have read in couple of questions what it is best not to show record id to users, but use another value, which doesn't give out any information about howmany record there is in the database, etc. I wanted to implement this, and after searching on google, surprisingly no solution was found.

So, my question is, is it possible to convert a (long, for example) sequence of strings to unique (or unique enough for at least million converts) sequence of numbers, is there any options available which I have no idea of? Just to show you an example:

 $uName = $this->newUsername;
 $publicId = $this->strToInt(somecomplexstring); // Outputs something like 13272992
 // Or feed with username
 $publicId = $this->strToInt($uName);
  • You could use uniqueid – B001ᛦ Feb 16 '17 at 09:34
  • Surely your question is back to front change the numerical ID `(integer)` into a human readable string that doesn't reveal the number of indexes ? How would your complex string tell me how many records there are? – Luke Feb 16 '17 at 09:36
  • uniqid — Generate a unique ID echo uniqid(); – JYoThI Feb 16 '17 at 09:37
  • I know, but you do realize what it generates random sequence of alphanumeric characters? While this is an option (even though it's output is quite long), I am looking to function which could output a sequence of integers by input string. – Metal Castles Feb 16 '17 at 09:37
  • @Luke Thanks for the idea (about converting integer to string). Complex stringis just a unique sequence of numbers and letters. – Metal Castles Feb 16 '17 at 09:45

3 Answers3

1

You might considering using something like a slug. So your user will have an unique id in the database but also an unique slug (random string, ex. TGqJItemU5TGqJItemU5f6S5VaCr2n). You can then use this slug instead of the id when presenting data to the browser.

maesbn
  • 207
  • 3
  • 6
0

As stated in a comment, you can use uniquid to get a unique string. this function will return an hexadecimal string.

If you need only numbers, you just have to convert the hex to a decimal number with this hexdec.

The final code will look like this :

hexdec(uniqid());

An other way to get an integer from a string is to use md5. This function also returns a hex string so you will have to use hexdec to get a decimal number

Please note that a md5 is not a unique string, there is a (very small) probability of accidental collision (1 in 340 undecillion, see How many random elements before MD5 produces collisions ? for more info)

Community
  • 1
  • 1
ᴄʀᴏᴢᴇᴛ
  • 2,939
  • 26
  • 44
-1

You can use:

md5(uniqid(rand(), true))