【问题标题】:PHP username validationPHP用户名验证
【发布时间】:2010-12-08 03:24:24
【问题描述】:

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

【问题讨论】:

    标签: php


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

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

    【讨论】:

    • 您好,感谢您的快速答复!我必须等待 10 分钟才能让我接受你的回答,所以我稍后再做。
    • preg_match 更适合这里,也不需要字符类。
    • 如果您只针对一个字符或特殊字符集 (\w),方括号是多余的
    • 另外\w 的定义取决于语言环境。如果 OP 只想允许英文字母,枚举它们会更安全。
    【解决方案2】:

    我推荐的最佳方法是:-

    $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;
        }
    }
    

    【讨论】:

      【解决方案3】:

      如果不关心长度,可以使用:

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

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

      【讨论】:

        【解决方案4】:

        试试这个

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

        【讨论】:

          【解决方案5】:

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

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

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

          【讨论】:

            【解决方案6】:

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

            它还将阻止指定为$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;
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-01-24
              • 2012-12-27
              • 2016-06-26
              • 2013-02-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多