English 中文(简体)
用户名称是否与用户名称相符的购买力平价核对?
原标题:PHP check if alternate username exists with foreach?
  • 时间:2012-05-19 08:22:36
  •  标签:
  • php
  • foreach

On my site, I have a registration page in PHP.
Someone can mimic Ashley by making AshIey.
I do not want this to happen. They replace lowercase L with capital I.
The string is $username and I wanted to check if it contained a capital I .
If it did, I would set a new string and use an SQL query to see if it existed.
But hat if the username has multiple capital I letters in it? Like DaryII.
I was thinking of doing a foreach statement but didn t know what to do.
If anyone could give me a little demo of what I need to do, I d be grateful.

最佳回答

I DID IT!!

if (preg_match( /[^A-Za-zds]|[^A-Zs]I|[A-Z]I[a-z]/ , $username)) {
die("That username is unavailable for registration.");
}
问题回答

正如Kasia Gogolek所建议的那样,你可能会对PHP提供的<编码>str_replace(功能感兴趣。 你可以这样利用:

$username = str_replace(I , l ,$username);

或者,你可能希望利用Levenshtein远程功能确定最接近的$username到名词典中显示。 只有在数据库没有匹配的情况下,你才能使用词典,即使你取代减少资源使用的特性。 PHP有这种功能,其名称是:levenshtein(-http://php.net/manual/en/Function.levenshtein.php>rel=“nofollow” 。

如果你能够确定贵轮功店主的储存程序,你可以采用以下例行做法(根据MySQL.5.16测试):

CREATE FUNCTION `LEVENSHTEIN`(`s1` VARCHAR(255), `s2` VARCHAR(255))
    RETURNS int(11)
    LANGUAGE SQL
    DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT   
BEGIN
  DECLARE s1_len, s2_len, i, j, c, c_temp, cost INT;
  DECLARE s1_char CHAR;
  DECLARE cv0, cv1 VARBINARY(256);
  SET s1_len = CHAR_LENGTH(s1), s2_len = CHAR_LENGTH(s2), cv1 = "0×00", j = 1, i = 1, c = 0;
  IF s1 = s2 THEN
    RETURN 0;
  ELSEIF s1_len = 0 THEN
    RETURN s2_len;
  ELSEIF s2_len = 0 THEN
    RETURN s1_len;
  ELSE
    WHILE j <= s2_len DO
      SET cv1 = CONCAT(cv1, UNHEX(HEX(j))), j = j + 1;
    END WHILE;
    WHILE i <= s1_len DO
      SET s1_char = SUBSTRING(s1, i, 1), c = i, cv0 = UNHEX(HEX(i)), j = 1;
      WHILE j <= s2_len DO
        SET c = c + 1;
        IF s1_char = SUBSTRING(s2, j, 1) THEN SET cost = 0; ELSE SET cost = 1; END IF;
        SET c_temp = CONV(HEX(SUBSTRING(cv1, j, 1)), 16, 10) + cost;
        IF c > c_temp THEN SET c = c_temp; END IF;
        SET c_temp = CONV(HEX(SUBSTRING(cv1, j+1, 1)), 16, 10) + 1;
        IF c > c_temp THEN SET c = c_temp; END IF;
        SET cv0 = CONCAT(cv0, UNHEX(HEX(c))), j = j + 1;
      END WHILE;
      SET cv1 = cv0, i = i + 1;
    END WHILE;
  END IF;
  RETURN c;
END

您的问:

SELECT t1.`key` FROM `dict` AS t1 WHERE levenshtein("patryk", t1.`key`) < 5

<patryk> 用户名变量。

EDIT I have just checked the query against the example input you provided (DaryII and Daryll) and it returns the correct row.

EDIT 2 You might also be interested in a percentage ratio. Use this function instead then:

CREATE FUNCTION levenshtein_ratio( s1 VARCHAR(255), s2 VARCHAR(255) ) 
  RETURNS INT 
  DETERMINISTIC 
  BEGIN 
    DECLARE s1_len, s2_len, max_len INT; 
    SET s1_len = LENGTH(s1), s2_len = LENGTH(s2); 
    IF s1_len > s2_len THEN  
      SET max_len = s1_len;  
    ELSE  
      SET max_len = s2_len;  
    END IF; 
    RETURN ROUND((1 - LEVENSHTEIN(s1, s2) / max_len) * 100); 
  END; 

这两项职能均从

rel=“nofollow”>str_replace 你们应当重新寻找。

你可以通过制定一条规则来避免这一问题,该规则只允许用户名称的下级信函,但第一封除外。 可以通过定期表达方式做到这一点:

if(preg_match( /^[A-Z]{0,1}[a-z]+$/ , $username)){
    echo  Username is allowed ;
}else{
    echo  Username is not allowed ;
}




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签