Skip to content

Instantly share code, notes, and snippets.

@lysender
Last active August 29, 2015 14:10
Show Gist options
  • Save lysender/1a04cc8c08edb4a7b88a to your computer and use it in GitHub Desktop.
Save lysender/1a04cc8c08edb4a7b88a to your computer and use it in GitHub Desktop.
[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]$
<?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