PHP: come creare un UUID

PHP: come creare un UUID

Possiamo creare un UUID con PHP.

La soluzione รจ la seguente:


function client_ip_to_hex( $ip = '' ) {
	$hex = '';
	if( $ip == '' ) $ip = getenv( 'REMOTE_ADDR' );
	$part = explode('.', $ip );
	for ( $i = 0; $i <= count( $part ) - 1; $i++ ) {
		$hex .= substr('0' . dechex( $part[$i] ),-2 );
	}
	return $hex;
}

function uuid( $server_id = 1 ) {
	$t = explode(' ', microtime() );
	return sprintf( '%04x-%08s-%08s-%04s-%04x%04x',
		$server_id,
		client_ip_to_hex(),
		substr('00000000' . dechex( $t[1] ),-8 ),
		substr('0000' . dechex( round($t[0]*65536 ) ),-4 ),
		mt_rand( 0,0xffff ), mt_rand( 0,0xffff ) );
}

Torna su