Generating confirmation code for an email confirmation

前端 未结 5 885
感情败类
感情败类 2020-12-02 05:29

Using PHP, what are some ways to generate a random confirmation code that can be stored in a DB and be used for email confirmation? I can\'t for the life of me think of a wa

5条回答
  •  天涯浪人
    2020-12-02 06:09

    $random_hash = md5(uniqid(rand(), true));
    

    That will be 32 alphanumeric characters long and unique. If you want it to be shorter just use substr():

    $random_hash = substr(md5(uniqid(rand(), true)), 16, 16); // 16 characters long
    

    Alternative methods to generate random data include:

    $random_hash = md5(openssl_random_pseudo_bytes(32));
    $random_hash = md5(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));
    
    // New in PHP7
    $random_hash = bin2hex(random_bytes(32));
    

提交回复
热议问题