I have a form that inserts items into a MySQL database upon submission. I have decided to create 16 bit IDs for the items using the random_bytes
method in PHP so that every time the form submits I already know the ID and don't have to re-query the database for it for it.
In order to insert the ID into a link in the page I have tried using the bin2hex
method. This normally works correctly. However, when the ID has trailing zeros the method seems to cut them off (passing in the direct result of the random_bytes
method). In other pages where I use the function on the same ID retrieved from the database it does not do this and works correctly.
In other words, if the ID ends in 000, it is shown correctly in the hexadecimal representation when bin2hex
is called on the database retrieved binary representation. However, the zeros are missing in the hexadecimal representation when the function is called on the direct output of the random_bytes
function.
Here is the initial code:
$uuid = random_bytes(16);
$uuidHex = bin2hex($uuid);
Later in the same script:
$GLOBALS["ID"] = '0x' . $uuidHex;
Script inside link href
attribute:
<?php echo $ID; ?>
Here is an example ID where the problem would occur:
0xa87ea4fc142fcdeasjf90j3771eda500