douji5746 2016-01-08 20:48
浏览 37

在PHP中登录表单的问题,删除特殊字符,但接受希腊字母

I am making a registration/login form for my site but i came up with a problem... Trying to make the input strings from name and surname forms as cleaner as possible.I tried many premade functions to search strings and don't let users to use specials chars like @#$%^&*(){}" etc but when user is writing their name or surname in greek chars the without special chars, the function reads special chars.Below is the code:

$required_fields = array('name', 'surname', 'email', 'password', 'confirm_password');
$wrong_chars = "0123456789!@#$%^&*()+=-[]';,./{}|:<>?~";

foreach($required_fields as $field) {
        if($_POST[$field] == '') {
          $errors[] = "All Fields are required.";
          break;
        }
    }   

     if(empty($errors)) {
         if(strpbrk($name,$wrong_chars) !== false) {
           $errors[] = "You can't use special characters on field: Name.";  
         }
         else if(strpbrk($surname,$wrong_chars) !== false){
             $errors[] = "You can't use special characters on field: Surame.";
         }
     }

Any help here and if there is any suggestion for a better algorithm would be appreciated.Thanks in advance.

  • 写回答

1条回答 默认 最新

  • donglu9872 2016-01-08 21:42
    关注

    You can't have this approch because a lot of members have name with specialchars but if you want to do this you can use ctype_alnum() function like this:

    $user = array();
    $required_fields = array('name', 'surname', 'email', 'password', 'confirm_password');
    
    foreach($required_fields as $field) {
        if(!array_key_exists($field,$_POST) || empty($_POST[$field])) {
            $errors[] = "$field field are required.";
        } else {
            $user[$field] = htmlentities($_POST[$field],ENT_QUOTES,'UTF-8');
        }
    }   
    $user = (object)$user;
    if(empty($errors)) {
        if(!ctype_alnum($user->name)) {
            $errors[] = "You can't use special characters on field: Name.";  
        } elseif(!ctype_alnum($user->surname)) {
            $errors[] = "You can't use special characters on field: Surame.";
        }
    }
    var_dump($errors);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计