PHP7 random number

I have the following snippet, using PHP7’s random_bytes function:
<?php $bytes = bin2hex(random_bytes(11)) ?>
<?= $bytes ?>
It works and generates a 22-char string, changing to another string every time I refresh the page.
But I have two problems:
(1) a typical output is something like: df17b533def6ab919542a5 ?>
How do I get rid of the trailing " ?>" which appears every time?
(2) the string is of numbers and lowercase letters.
I would prefer a mixture of uppercase, lowercase, and numbers.
How can the code be changed to produce this?

1.) perhaps try terminating your line(s).

1 Like

Thank you. I assume by “terminating your lines” you meant to put a “;” before the ?>

Do you have any ideas for 2)??

Well not sure if the HEX value returned will EVERY have UPPERCASE letters…

So you would have to maybe maybe make your own functions and randomly select a few characters and make them upprecase.

Or perhaps generate your own:

$permitted_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';

function generate_string($input, $strength = 16) {
    $input_length = strlen($input);
    $random_string = '';
    for($i = 0; $i < $strength; $i++) {
        $random_character = $input[mt_rand(0, $input_length - 1)];
        $random_string .= $random_character;
    }

    return $random_string;
}

// Output: iNCHNGzByPjhApvn7XBD
echo generate_string($permitted_chars, 20);

// Output: Jp8iVNhZXhUdSlPi1sMNF7hOfmEWYl2UIMO9YqA4faJmS52iXdtlA3YyCfSlAbLYzjr0mzCWWQ7M8AgqDn2aumHoamsUtjZNhBfU

echo generate_string($permitted_chars, 100);

More info generating random/secure numbers/strings:

What is your use case for doing this?

My “use case” is this:
I want to generate a “Unique ID number” without using MySQL (or any other RDBM):
– uniqueness is desirable, but not absolutely essential
– I found this code on StackOveflow:

<?php $bytes = bin2hex(random_bytes(11)) ?> <?= $bytes ?>

which uses PHP7 (I am using PHP7.2) to generate a (fairly) random number, and I am relying on the 22-char length of the generated hex value to make it unlikely to create duplicate values for the few thousands of times I expect it to be used.
(I could in future add code to check against an existing list of values and regenerate in case of duplication).
I thought that a mixture of upper & lowercase letters (plus numbers) would look better and maybe give a wider range of values, but i’m not sure if that can be done without greatly complicating what is after all a very simple piece of code which works albeit without uppercase!!

The randomness is provided by random_bytes; bin2hex - or any other encoding function - is just representation. Using more characters won’t give you more randomness.

You didn’t give me a use case. You just told me what you are trying to do which I already knew. What is this random number used for? Why are you trying to do this?

I have some form-to-email php code which sends an email to me when a visitor to my website clicks “Submit” having completed some form fields on my webpage.
What I need is a unique id which can be generated at the time of “Submit”, which I can use to identify multiple submissions from each other.

Its not for security reasons/encryption… and just personal, internal tracking issues, then why bother?

or use the similar approach I posted above where you generate your OWN output with the lower and upper case characters.

I was hoping in my ignorance that someone would say “add xxxx” and you’d get what you want.
Thanks for your help, but I was really trying to keep it simple!!
Stay safe.

Sponsor our Newsletter | Privacy Policy | Terms of Service