PHP生成惟一標識ID

1.不太好的方法php

echo uniqid(); //13位的字符串
echo uniqid("PHP_"); //固然你能夠加上前綴
echo uniqid("PHP_", true)."\n";////若是第二個參數more_entropy爲true則生成23位字符串
echo md5(uniqid())."\n"; //這是第一種簡單的方法,固然用sha1()函數也能夠。
echo md5(time() . mt_rand(1,1000000))."\n";//第二種,利用時間戳的方法。函數

 

2.官方com_create_guid()參考手冊有用戶提供的方法,結果相似:{E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7}ui

function guid(){  
        if (function_exists('com_create_guid')){  
            return com_create_guid();  
        }else{  
            mt_srand((double)microtime()*10000);//optional for php 4.2.0 and up.  
            $charid = strtoupper(md5(uniqid(rand(), true)));  
            $hyphen = chr(45);// "-"  
            $uuid = chr(123)// "{"  
                    .substr($charid, 0, 8).$hyphen  
                    .substr($charid, 8, 4).$hyphen  
                    .substr($charid,12, 4).$hyphen  
                    .substr($charid,16, 4).$hyphen  
                    .substr($charid,20,12)  
                    .chr(125);// "}"  
            return $uuid;  
        }  
    }  
    echo guid();

3.官方uniqid()參考手冊有用戶提供的方法,結果相似:{E2DFFFB3-571E-6CFC-4B5C-9FEDAAF2EFD7}spa

function create_guid($namespace = '') {      
    static $guid = '';  
    $uid = uniqid("", true);  
    $data = $namespace;  
    $data .= $_SERVER['REQUEST_TIME'];  
    $data .= $_SERVER['HTTP_USER_AGENT'];  
    $data .= $_SERVER['LOCAL_ADDR'];  
    $data .= $_SERVER['LOCAL_PORT'];  
    $data .= $_SERVER['REMOTE_ADDR'];  
    $data .= $_SERVER['REMOTE_PORT'];  
    $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));  
    $guid = substr($hash,  0,  8) .  
            '-' .  
            substr($hash,  8,  4) .  
            '-' .  
            substr($hash, 12,  4) .  
            '-' .  
            substr($hash, 16,  4) .  
            '-' .  
            substr($hash, 20, 12) ;  
    return $guid;  
  }
相關文章
相關標籤/搜索