php

PHP program to generate a token that is cryptographically secured

You can use PHP openssl_random_pseudo_bytes() or random_bytes() methods to generate random bytes of a fixed number and then convert them to readable format using the bin2hex() method.

<?php
    $token = bin2hex(openssl_random_pseudo_bytes(20));
    echo "$token";

    // If you are using PHP 7 - You can also use
    $token = bin2hex(random_bytes(20));
    echo $token;
?>
Output
d2796f2a9cc1426f49446f0991c0c19c9d929c65
5c44c782bbe8d1c37d513e9390b64c2c457e74bd

The code will generate a new random token each time you run the code.

Was this helpful?