Hash to identify url?

It is necessary to memorize some URLs visited by the user on the site in order to return the user to this address if necessary. For example, the user went to the example.com?part=cart&order=1 page (basket with orders), the address of this page was recorded in the database and the hash of this address returned, for example, 5f41402dbc4b2a76b9231d911017c592 . For each link to an order item, a backurl parameter is added to the link address. Ie, in order to edit a product from the basket, the link to it is of the following type:

 example.com?part=goods&id=57483&backurl=5f41402dbc4b2a76b9231d911017c592 

Now, after any actions with this product, by pressing the "save" button, the user will be returned to the page with the hash

 5f41402dbc4b2a76b9231d911017c592: example.com?part=cart&order=1. 

Which hash is better to use in this case? The smaller it is (shorter) - the better, I intend to use md5 - its length is 32 characters, and cryptographic resistance does not matter much, since it is not the password that is encrypted, but only the address of the link + the user cannot follow the link of another user, as in the database with the hash is recorded another user ID. I would like something like tinyurl.com

  • And why not use the auto-increment identifier from the database? - Anton Shchyrov
  • Because you do not want to "shine" the number of links, their growth, etc. - Miron

2 answers 2

I used the @Anton Shchyrov tip to use the link address (use the auto-increment identifier from the database), with only a few additions: the digital id is hidden using Hashids . It looks much better already:

 example.com?part=goods&id=57483&backurl=ObA1k253 

Hashids allows you not only to encode id, but also to perform the reverse operation of decryption, so these hashes do not need to be stored in the database.

To check for doubles (so as not to write duplicate identical links in the database) I decided to use hash md5. The chance to catch a collision when 2 different links have an identical md5 hash is minimal and tends to zero.

    Take a timestamp. They will be unique and do not require any complex generation mechanism. This is how you can get a millisecond stimestamp:

     $milliseconds = round(microtime(true) * 1000); 
    • Timestamp, how much I remember, is recorded to the nearest seconds? If so, then the likelihood that there will be doubles is quite high - Miron
    • there are different timestamp. There are who write up to milliseconds. I added the code in the answer. - Mikhail Vaysman
    • Ok, thanks, note. - Miron
    • @Miron If you are given an exhaustive answer, mark it as correct (checkmark the selected answer). - Mikhail Vaysman
    • If it were exhaustive - I would have done so :) - Miron