I need this function in server side. The server stores user's license as well as the hardware id. If user requests license again, the server would check if a previous hwid the same user provided is the same as current hwid. If so, the related stored license is returned to user, so server doesn't need to generate a license each time and can manager and limit the number of licenses a user can have.
@Admin, could you check the implementation if it's correct?
Code: Select all
<?php
// $hwid and $other_hwid are both valid base64 encoded hardware id strings.
function CheckHwidSame($hwid, $other_hwid) {
$hwdata = HwidDecode($hwid);
$other_hwdata = HwidDecode($other_hwid);
if (empty($hwdata) || empty($other_hwdata)) {
return false;
}
return CheckHwdataSame($hwdata, $other_hwdata);
}
function HwidDecode($hwid)
{
if (empty($hwid))
return FALSE;
// a decoded hwid is an array of devices. Each device is a 32-bit unsigned int value.
$data = unpack("V*", base64_decode($hwid));
//x64 system PHP bug workaround, to keep numbers unsigned
foreach ($data as $i => $num)
{
if ($data[$i] < 0)
$data[$i] += 4294967296;
}
return $data;
}
function CheckHwdataSame($hwdata, $other_hwdata) {
$common = array();
// $common would store common devices of the 2 hwids.
foreach ($hwdata as $device) {
foreach($other_hwdata as $other_device) {
if ($device == $other_device) {
array_push($common, $device);
break;
}
}
}
// There're 4 types of devices, specified by the least 2 bits.
// To prevent duplications, use $type array to mask occurrences.
$type = array(false, false, false, false);
foreach ($common as $device) {
$type[$device & 3] = true;
}
// 2 hwids are the same if type 0 exists and 2 of the remaining 3 types exist.
$two_of_three = ($type[1] && $type[2]) || ($type[1] && $type[3]) || ($type[2] && $type[3]);
return $type[0] && $two_of_three;
}
?>