在php中完成表单验证

在php中完成表单验证,php,validation,Php,Validation,我有这个php代码与数据库关联,我需要在这里做一个完整的电子邮件和名称验证 基于这段代码,我该怎么做,因为我的代码在这里有一些问题 1) 名称键没有(//)或任何符号作为正确的名称 2) 电子邮件密钥是有效的电子邮件,因为我们在这里所做的只是确保有@symbol,如果我键入电子邮件hhhh@hhh.com或者即使没有(.com),它也将有效 if(array_key_exists("submit",$_POST)){ $link = mysqli_connect("localhost","r

我有这个php代码与数据库关联,我需要在这里做一个完整的电子邮件和名称验证 基于这段代码,我该怎么做,因为我的代码在这里有一些问题

1) 名称键没有(//)或任何符号作为正确的名称

2) 电子邮件密钥是有效的电子邮件,因为我们在这里所做的只是确保有@symbol,如果我键入电子邮件hhhh@hhh.com或者即使没有(.com),它也将有效

if(array_key_exists("submit",$_POST)){
  $link = mysqli_connect("localhost","root","123456789","users");

  if(mysqli_connect_error()){
    die("There is a problem in connecting to database");
  }

  if(!$_POST['name']){
    $error .="<p>Your Full name is required</p><br>";
  }

  if(!$_POST['email']){
    $error .="<p>Your email address is required</p><br>";
  }

  if(!$_POST['password']){
    $error .="<p>Your password is required</p><br>";
  }

  if($error !=""){
    $error = "<p>There were errors in your form</p><br>".$error;
  }
}
if(数组\键\存在(“提交”、$\发布)){
$link=mysqli_connect(“localhost”、“root”、“123456789”、“users”);
if(mysqli\u connect\u error()){
die(“连接到数据库时出现问题”);
}
如果(!$\u POST['name'])){
$error.=“需要您的全名”


”; } 如果(!$\u POST['email']){ $error.=“您的电子邮件地址是必需的


”; } 如果(!$\u POST['password']){ $error.=“需要您的密码”


”; } 如果($error!=“”){ $error=“您的表单中有错误


”$error; } }
您可以使用此功能进行验证:

 function filtervariable($string,$type,$method) {
    //function for sanitizing variables using PHPs built-in filter methods
    $validEmail = false;

    if ($method == 'sanitize') {
        $filtermethod = 'FILTER_SANITIZE_';
    } elseif ($method == 'validate') {
        $filtermethod = 'FILTER_VALIDATE_';
    } else {
        return;
    }
    switch ($type) {
        case 'email':
        case 'string':
        case 'number_int':
        case 'int':
        case 'special_chars':
        case 'url':
        $filtertype = $filtermethod.strtoupper($type);
        break;
    }

    if ($filtertype == 'FILTER_VALIDATE_EMAIL' && !empty($string)) {
        list($local,$domain) = explode('@',$string);

        $localLength = strlen($local);
        $domainLength = strlen($domain);

        $checkLocal = explode('.',$domain);

        if (($localLength > 0 && $localLength < 65) && ($domainLength > 3 && $domainLength < 256) && (checkdnsrr($domain,'MX') || checkdnsrr($domain,'A') || ($checkLocal[1] == 'loc' || $checkLocal[1] == 'dev' || $checkLocal[1] == 'srv'))) { // check for "loc, dev, srv" added to cater for specific problems with local setups
            $validEmail = true;
        } else {
            $validEmail = false;
        }
    }
    if (($filtertype == 'FILTER_VALIDATE_EMAIL' && $validEmail) || $filtertype != 'FILTER_VALIDATE_EMAIL') {
        return filter_var($string, constant($filtertype));
    } else {
        return false;
    }
}
函数filtervariable($string,$type,$method){
//用于使用PHPs内置筛选器方法清理变量的函数
$validEmail=false;
如果($method=='sanitize'){
$filtermethod='FILTER_SANITIZE_';
}elseif($method=='validate'){
$filtermethod='FILTER\u VALIDATE\u';
}否则{
返回;
}
交换机($类型){
“电子邮件”案例:
大小写“string”:
“编号”案例:
案例“int”:
“特殊字符”案例:
案例“url”:
$filtertype=$filtermethod.strToper($type);
打破
}
如果($filtertype=='FILTER\u VALIDATE\u EMAIL'&&&!empty($string)){
列表($local,$domain)=分解('@',$string);
$localLength=strlen($local);
$domainLength=strlen($domain);
$checkLocal=分解('.',$domain);
如果($localLength>0&&$localLength<65)&($domainLength>3&&$domainLength<256)&($checkdnsrr($domain,'MX')| | checkdnsr($domain,'A')| |($checkLocal[1]='loc'|$checkLocal[1]='dev'|$checkLocal[1]='srv')){//检查添加的“loc dev,srv”,以解决本地设置的特定问题
$validEmail=true;
}否则{
$validEmail=false;
}
}
如果($filtertype=='FILTER\u VALIDATE\u EMAIL'&&&$validEmail)| |$filtertype!='FILTER\u VALIDATE\u EMAIL'){
返回filter_var($string,constant($filtertype));
}否则{
返回false;
}
}
然后像这样使用它:
$email=filtervariable($registeremail,'email','validate')


成功时返回“true”,失败时返回“false”。

不要用代码链接到外部站点。将代码粘贴到这里。请重新措辞/重写您的问题,因为它们在当前的表单中完全没有意义。我刚刚将我使用的表单验证放入您想知道的内容中,如何确保名称和电子邮件不包含无效字符,以及电子邮件是否有效?这就是你要问的吗?是的!这就是我需要的。你有没有尝试/搜索过任何东西来解决这个问题?请你解释一下你的答案?亲爱的junkfoodjunkie,你的意思是我可以把这个函数放在这个php代码的任何地方?!我还应该使用$email=filtervariable($registeremail,'email','validate');正如您所做的那样或$_POST['email']=filtervariable($registeremail,'email','validate');您可以将函数放在上面的php文件的底部,或者放在包含php函数的文件中,只要它在执行验证的文件中可用。你真的应该读一些基本的PHP教程