Comment créer un ID unique ?

PHP propose la fonction uniqid pour générer des identifiants uniques. Préfixe à ajouter à l'identifiant. Mettre une chaîne vide ou ne rien mettre si vous ne souhaitez pas de préfixe.
<?php
public function createGUID(){
// append the IP address w/o periods to the front of the unique ID
if(isset($_SERVER['REMOTE_ADDR'])){
$ip = $_SERVER['REMOTE_ADDR'];
} else{
$ip = '';
}
$varGUID = strtoupper(str_replace('.', '', uniqid($ip, TRUE)));
$varGUID = str_replace('::','_',$varGUID);
// add "-"
// AC041D36-43E8-414E-BD05-F733BCD7298E
$varGUID = substr($varGUID,0,8).'-'.substr($varGUID,8,4).'-'.substr($varGUID,12,4).'-'.substr($varGUID,16,4).'-'.substr($varGUID,20,12);
// return the GUID as the function value
return $varGUID;
}
?>
Catégories
Savoirs les plus récents
-
Création de tableaux en HTML
HTML5 -
PHP DateTime : créez, comparez et formatez des dates
PHP -
Correction algorithme : Généalogie
Algorithmes -
Correction algorithme : Coupe du monde
Algorithmes -
Correction algorithme : Découpage et collage
Algorithmes