Kryptix Posted November 22, 2009 Share Posted November 22, 2009 I run a online game which wasn't coded by me originally. I've taken over the project and it uses a custom hash to encode a username into. Sometimes the encode_username() fails, it times out after 60 seconds. A player just tried to create the name 'Snoopy89' and it failed, so I tried and it worked first time. I then tried on his machine changing the case randomly and eventually it worked, even though on the 2nd line it strtolower() the string. It also fails if there's a underscore in the username, so I tried adding str_replace("_", " ", $username) but it still fails. For example, if I enter "TEST_ING" it'll fail even with str_replace() in place, but if I try "TEST ING" it'll work fine. I've done so much testing using this but it still sometimes randomly fails, and I can't figure out why. Unfortunately, there's over 800,000 database records using this format, and the entire game is coded to use this hash so it'd be very difficult changing it now. Other private servers use the exact same function with no problems. function encode_username($username) { $username = strtolower($username); $clean = ''; for($i = 0;$i < strlen($username);$i++) { $c = ord($username{$i}); if($c >= 97 && $c <= 122) { $clean .= chr($c); } else if($c >= 48 && $c <= 57) { $clean .= chr($c); } else { $clean .= ' '; } } $clean = trim($clean); if(strlen($clean) > 12) { $clean = substr($clean, 0, 12); } $hash = '0'; for($i = 0;$i < strlen($clean);$i++) { $c = ord($clean{$i}); $hash = bcmul($hash, 37); if($c >= 97 && $c <= 122) { $hash = bcadd($hash, (1 + $c) - 97); } else if($c >= 48 && $c <= 57) { $hash = bcadd($hash, (27 + $c) - 48); } } return $hash; } function decode_username($hash) { if(!$hash) { return 'invalid_name'; } $username = ''; while($hash) { $i = bcmod($hash, 37); $hash = bcdiv($hash, 37); if($i == '0') { $username = ' '.$username; } else if($i < 27) { if(bcmod($hash, 37) == '0') { $username = chr(($i + 65) - 1).$username; } else { $username = chr(($i + 97) - 1).$username; } } else { $username = chr(($i + 48) - 27).$username; } } return $username; } The code I'm using to use this function is: $encoded_user = encode_username(trim($_POST['char_username'])); $decoded_user = decode_username($encoded_user); Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampplite\htdocs\webinc\server_functions.php on line 72 Line 72 of server_functions.php is: if($i == '0') { $username = ' '.$username; } Any help would be greatly appreciated. Quote Link to comment Share on other sites More sharing options...
emopoops Posted November 22, 2009 Share Posted November 22, 2009 tell them not to use underscores. Quote Link to comment Share on other sites More sharing options...
Kryptix Posted November 22, 2009 Author Share Posted November 22, 2009 tell them not to use underscores. ... Quote Link to comment Share on other sites More sharing options...
Gayner Posted November 22, 2009 Share Posted November 22, 2009 tell them not to use underscores. Troll Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.