Im trying to map urls to numbers in a range [0,50] for porting, that should spread evenly over the range so that no port gets hammered.
Below is my code but I can figure out why modulus isnt working for me.
$fetch_url = "http://74.125.224.72/profile/user";
$hash = sha1($fetch_url);
$hasher = substr($hash,1,50);
$port_index = hexdec($hasher)%50;
$port = 8700 + $port_index;
Seems like everything works up to the $port_index which returns 0. Keep in mind “user” is actual username that is different every time.
The final goal is written below:
http://74.125.224.72/profile/j - port = 8701
http://74.125.224.72/profile/m - port = 8702
http://74.125.224.72/profile/p - port = 8703
And should be that way everytime the user logs in and hits their profile.
Any ideas?
Thanks
-J
I believe the issue is that the hexdec conversion of a sha1 hash is so huge that PHP kinda stops processing it as a number. You should just trim off the last few characters of the hash and hexdec that. It seems like you might have been trying with your substr, but a sha1 is 40 characters and you did a 50 substr. Was that 50 a mistake?
Because of this, hexdec returns something like ‘5.4627305075531E+46’ which won’t run through modulus properly. Try: