It takes a long time to figure out what was causing malfunctioning a website of mine when migrating to a better hosting subscription.
I use a 'self-made' uniqueId generator to generate everything that must be unique but this uniqueness is not random. I use this to communicate between several services, generate reproducible unique 'numbers' for files, articles and so on.
This is the function I have made and never had problems with (I think it never runs on a 64bit system before?) to generate an unique id. I know this uniqueness is limited (64.000) but never lead to a problem until now.
function suGetHashCode($s)
{
$hash=0;
$c=(is_string($s))?strlen($s):0;
$i=0;
while($i<$c)
{
$hash = (($hash << 5)-$hash)+ord($s{$i++});
//hash = hash & hash; // Convert to 32bit integer
}
return ( $hash < 0 )?(($hash*-1)+0xFFFFFFFF):$hash; // convert to unsigned int
}
function suUniqueId( $s, $bAddLen = false )
{
$i = base_convert( suGetHashCode( $s ), 10, 32 );
if( $bAddLen && is_string($s) )
{ $i.=('-'.suGetLz( dechex( strlen($s)*4 ), 3 )); }
return $i;
}
function suGetLz( $i, $iMaxLen ) // Leading zero
{
if( !is_numeric( $i ) || $i < 0 || $iMaxLen <= 0 )
{ return $i; }
$c = strlen( $i );
while( $c < $iMaxLen )
{ $c++; $i='0'.$i; }
return $i;
}
The max int value of an integer is on the new system:
PHP_INT_MAX = 9223372036854775807
On other system(s) it is:
PHP_INT_MAX = 2147483647
Well, I am not a math person, I think this is causing the problem because of the 0xFFFFFFFF increment when negative (I think it will be never negative on this new system).
But how can I change the function that it produces the same unique id's like on other systems?
For example: It produces the same id for different strings on the new hosting server:
$sThisUrl = '<censored>';
var_dump( suUniqueId($sThisUrl) ); // Produce: 1l5kc37uicb
$sThisUrl = '<censored>';
var_dump( suUniqueId($sThisUrl) ); // Produce the same id as above: 1l5kc37uicb
But, this must be like on older systems:
$sThisUrl = '<censored>';
var_dump( suUniqueId($sThisUrl) ); // Produce: a46q6nd
$sThisUrl = '<censored>';
var_dump( suUniqueId($sThisUrl) ); // Produce: 2mirj1h
Notice: The string is seperate into parts to avoid stackoverflow see this a link.
EDIT: Removed filenames
Does anyone how to deal with this problem?