开发者

What is the purpose of the crc function in php

开发者 https://www.devze.com 2023-01-16 10:48 出处:网络
I have a variable开发者_开发技巧 $offset = abs(crc32($_SERVER[\"SERVER_NAME\"]) % 500); and I do not understand it\'s place in the script. All it seems to do is generate a random number based on th

I have a variable开发者_开发技巧

   $offset = abs(crc32($_SERVER["SERVER_NAME"]) % 500);

and I do not understand it's place in the script. All it seems to do is generate a random number based on the server name (e.g. www.example.com). Is that a correct interpretation of this variable?

Thanks


crc32 creates a Cyclic redundancy check of the name, and then moding by 500 further reduces it to a number between 0 and 499. The first part is basically a signature of the server name - it's not a random number exactly (in it's original form CRC could be used to rebuild data that got corrupted), it's being used more like a hash here. The second part is reducing the size/scale of that hash so that every server that visits gets a number between 0 and 499. The hope is that it's rare that number is the same for two servers, although there's no real guarantees.


Yes, it makes a number between 0 and 499 which only differs if the server name differs.

  • Crc32 gets an numeric checksum from a string.
  • % 500 makes it between -499 and 499.
  • abs makes negative numbers positive.
0

精彩评论

暂无评论...
验证码 换一张
取 消