this is my code:
$valid = true;
$len = strlen($packet->username);
if($len > 16 or $len < 3){
$valid = false;
}
for($i = 0; $i < $len and $valid; ++$i){
$c = ord($packet->username{$i});
if(($c >= ord("a") and $c <= ord("z")) or
($c >= ord("A") and $c <= ord("Z")) or
($c >= ord("0") and $c <= ord("9")) or $c === ord("_") ){
continue;
}
$valid = false;
break;
}
if(!$valid or $this->iusername === "rcon" or $this->iusername === "console"){
$this->close("", "§ePlease make sure your username is longer then §a3§e characters\n§eand only uses numbers and letters§7.");
return;
}
It checks usernames to make sure it is valid. This code is too slow, and it loops $len times which causes seconds wasted doing nothing on my main thread. Anyone know any solutions to make this faster & possibly remove the loop?
use this:
$username = 'admin';
$valid = preg_match('/^[\w\d]{3,16}$/',$username);
if(!$valid){
echo 'Bad character in your username !';
}
$invalidUsername = array(
'admin',
'root',
'localhost'
);
if(in_array($username, $invalidUsername)) {
echo "Username is not valid !";
}
return true;
Simple regex:
$valid = (preg_match('/[^A-Z0-9_]/i', $username) === 0);
Check for anything that ISN'T alphabetical/numerical/underscore. If nothing is found (0 matches), then the username is valid. Otherwise it's invalid.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.