Last active
August 29, 2015 14:10
-
-
Save lysender/1a04cc8c08edb4a7b88a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[vagrant@localhost path2dx]$ php testuniqid.php | |
ECL00002-7f01-547c7274-5002 | |
ECL00002-0801-547c7274-5002 | |
ECL00002-0c65-547c7274-5002 | |
ECL00002-f4fb-547c7274-5002 | |
ECL00002-fafa-547c7274-5002 | |
[vagrant@localhost path2dx]$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class SomeUniqueId { | |
protected $strId = ''; | |
public function __construct($ip, $timestamp = null) | |
{ | |
$ids = array( | |
$this->generateLocationId(), | |
$this->generateHostId($ip), | |
$this->generateEpochId($timestamp), | |
$this->generateHexId(), | |
); | |
$this->strId = implode('-', $ids); | |
} | |
protected function generateLocationId() | |
{ | |
// Not sure | |
return 'ECL00002'; | |
} | |
protected function generateHostId($ip) | |
{ | |
$ips = explode('.', $ip); | |
$ips = array_map('intval', $ips); | |
$x1 = sprintf('%02x', $ips[0] ^ $ips[1] ^ $ips[2]); | |
$x2 = sprintf('%02x', $ips[3]); | |
$ret = $x1.$x2; | |
return $ret; | |
} | |
protected function generateEpochId($timestamp = null) | |
{ | |
if ($timestamp === null) { | |
$timestamp = time(); | |
} | |
// Make sure they are integers | |
$timestamp = (int) $timestamp; | |
return dechex($timestamp); | |
} | |
protected function generateHexId() | |
{ | |
// Not sure | |
return '5002'; | |
} | |
public function __toString() | |
{ | |
return $this->strId; | |
} | |
} | |
$id1 = new SomeUniqueId('127.0.0.1', time()); | |
$id2 = new SomeUniqueId('10.0.2.1', time()); | |
$id3 = new SomeUniqueId('192.168.100.101', time()); | |
$id4 = new SomeUniqueId('250.220.210.251', time()); | |
$id5 = new SomeUniqueId('251.252.253.250', time()); | |
echo $id1."\n"; | |
echo $id2."\n"; | |
echo $id3."\n"; | |
echo $id4."\n"; | |
echo $id5."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment