Generating confirmation code for an email confirmation

前端 未结 5 884
感情败类
感情败类 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:01

    Decided I need something a little more robust and added functionality. So this is what I came up with.

    /**
     * Hash Gen 
     * @author Kyle Coots
     * @version    1.0
     * Allow you to create a unique hash with a maximum value of 32.
     * Hash Gen uses phps substr, md5, uniqid, and rand to generate a unique 
     * id or hash and allow you to have some added functionality.
     * 
     * @see subtr()
     * @see md5()
     * @see uniqid()
     * @see rand()
     *  
     * You can also supply a hash to be prefixed or appened
     * to the hash. hash[optional] is by default appened to the hash 
     * unless the param prefix[optional] is set to prefix[true].     
     * 
     * @param start[optional]
     * @param end[optional]
     * @param hash[optional]
     * @param prefix bool[optional]
     * 
     * @return string a unique string max[32] character
     */
    function hash_gen($start = null, $end = 0, $hash = FALSE, $prefix = FALSE){
    
        // start IS set NO hash
        if( isset($start, $end) && ($hash == FALSE) ){
    
            $md_hash = substr(md5(uniqid(rand(), true)), $start, $end);
            $new_hash = $md_hash;
    
        }else //start IS set WITH hash NOT prefixing
        if( isset($start, $end) && ($hash != FALSE) && ($prefix == FALSE) ){
    
            $md_hash = substr(md5(uniqid(rand(), true)), $start, $end);
            $new_hash = $md_hash.$hash;
    
        }else //start NOT set WITH hash NOT prefixing 
        if( !isset($start, $end) && ($hash != FALSE) && ($prefix == FALSE) ){
    
            $md_hash = md5(uniqid(rand(), true));
            $new_hash = $md_hash.$hash;
    
        }else //start IS set WITH hash IS prefixing 
        if( isset($start, $end) && ($hash != FALSE) && ($prefix == TRUE) ){
    
            $md_hash = substr(md5(uniqid(rand(), true)), $start, $end);
            $new_hash = $hash.$md_hash;
    
        }else //start NOT set WITH hash IS prefixing
        if( !isset($start, $end) && ($hash != FALSE) && ($prefix == TRUE) ){
    
            $md_hash = md5(uniqid(rand(), true));
            $new_hash = $hash.$md_hash;
    
        }else{
    
            $new_hash = md5(uniqid(rand(), true));
    
        }
    
        return $new_hash;
    
     } 
    

提交回复
热议问题