PHP 用户名验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4383878/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:53:38  来源:igfitidea点击:

PHP username validation

php

提问by Adam Thompson

I am working on writing a PHP login system. I have everything that I need working, but I would like to verify that a username entered during the registration only contains alphanumeric characters. So how could I take a variable, say $username, and ensure that it contained only alphanumeric characters?

我正在编写一个 PHP 登录系统。我有我需要工作的一切,但我想验证在注册过程中输入的用户名是否只包含字母数字字符。那么我怎样才能使用一个变量,比如 $username,并确保它只包含字母数字字符呢?

回答by Ish

if(preg_match('/^\w{5,}$/', $username)) { // \w equals "[0-9A-Za-z_]"
    // valid username, alphanumeric & longer than or equals 5 chars
}

OR

或者

if(preg_match('/^[a-zA-Z0-9]{5,}$/', $username)) { // for english chars + numbers only
    // valid username, alphanumeric & longer than or equals 5 chars
}

回答by Matthew Mucklo

If you don't care about the length, you can use:

如果你不在乎长度,你可以使用:

if (ctype_alnum($username)) {
   // Username is valid
}

http://www.php.net/manual/en/function.ctype-alnum.php

http://www.php.net/manual/en/function.ctype-alnum.php

回答by ifreelancer.asia

The Best way I recommend is this :-

我推荐的最好方法是:-

$str = "";
function validate_username($str) 
{
    $allowed = array(".", "-", "_"); // you can add here more value, you want to allow.
    if(ctype_alnum(str_replace($allowed, '', $str ))) {
        return $str;
    } else {
        $str = "Invalid Username";
        return $str;
    }
}

回答by The Unicodist

If you are allowing the basic alpha-numeric username, You can check alphanumeric value with a predefined template like this:

如果您允许基本的字母数字用户名,您可以使用预定义的模板检查字母数字值,如下所示:

preg_match([[:alnum:]],$username)&&!preg_match([[:space:]],$username)

The second part returns false if the string contains any spaces.

如果字符串包含任何空格,则第二部分返回 false。

回答by muratgozel

A little bit late but I am using the following function to test usernames or other types of alphanum-like strings. It tests alphanum chars only by default but you can add more characters such as . (dot), - (dash) or _ (underscore) to the whitelist.

有点晚了,但我正在使用以下函数来测试用户名或其他类型的类似字母的字符串。默认情况下,它仅测试字母字符,但您可以添加更多字符,例如 . (点)、-(破折号)或 _(下划线)到白名单。

It will also prevent consecutive chars for the chars specified as $more_chars.

它还将防止指定为的字符的连续字符$more_chars

function valid_alphanum_string($str, $more_chars = '') {
  # check type
  if (!is_string($str)) return false;

  # handle allowed chars
  if (mb_strlen($more_chars) > 0) {
    # don't allow ^, ] and \ in allowed chars
    $more_chars = str_replace(array('^', ']', '\'), '', $more_chars);

    # escape dash
    $escaped_chars = strpos($more_chars, '-') !== false
      ? str_replace('-', '\-', $more_chars)
      : $more_chars;

    # allowed chars must be non-consecutive
    for ($i=0; $i < mb_strlen($more_chars); $i++) {
      $consecutive_test = preg_match('/[' . $more_chars[$i] . '][' . $escaped_chars . ']/', $str);
      if ($consecutive_test === 1) return false;
    }

    # allowed chars shouldn't be at the start and the end of the string
    if (strpos($more_chars, $str[0]) !== false) return false;
    if (strpos($more_chars, $str[mb_strlen($str) - 1])) return false;
  }
  else $escaped_chars = $more_chars;

  $result = preg_match('/^[a-zA-Z0-9' . $escaped_chars . ']{4,}$/', $str);

  return $result === 1 ? true : false;
}

回答by user516957

try this

尝试这个

function filterName ($name, $filter = "[^a-zA-Z0-9\-\_\.]"){
    return preg_match("~" . $filter . "~iU", $name) ? false : true;
}

if ( !filterName ($name) ){
 print "Not a valid name";
}