简体   繁体   English

如何验证PHP中的用户输入

[英]How do i validate user input in PHP

I'm looking for a PHP class that will help me validate my user input from a form. 我正在寻找一个PHP类,它将帮助我验证来自表单的用户输入。 For example, making sure the user has entered the mandatory fields, that the fields did not exceed their maximum length, that the user has entered a number in the "age" field and so on... If not i want to display the user an error and log that error to a database. 例如,确保用户输入了必填字段,字段未超过其最大长度,用户已在“年龄”字段中输入了数字,依此类推...如果没有,我想显示用户错误并将该错误记录到数据库中。 also, if possible with the same class to prevent sql injection & xss from user inputs. 另外,如果可能的话,使用相同的类来防止用户输入的sql注入&xss。 Is there a PHP 5 class that will help me do all of these tasks? 是否有PHP 5类可以帮助我完成所有这些任务? (Or multiple classes?) (或多个课程?)

Do you mean to say something like you want to check if the user has entered all the fields correctly on the server side using PHP? 您是说要检查用户是否使用PHP在服务器端正确输入了所有字段吗? If yes, then your method is quite inefficient as it will increase the traffic and load on the server for wrong submissions. 如果是,则您的方法效率很低,因为它将增加流量和错误提交的服务器负载。 So what generally is done is a Javascript client side script is used to validate the forms before submission. 因此,通常要做的是在提交之前使用Javascript客户端脚本来验证表单。 This link can help you to implement a simple client side validation in Javascript. 该链接可以帮助您使用Javascript实现简单的客户端验证。 http://www.elated.com/articles/form-validation-with-javascript/ http://www.elated.com/articles/form-validation-with-javascript/

As deceze pointed out, if you need server side validation using PHP, then this link has a simple example on how to do it using some built in functions. 正如deceze指出的那样,如果您需要使用PHP进行服务器端验证,那么此链接提供了一个简单的示例,说明了如何使用一些内置函数来进行验证。 http://www.htmlcenter.com/blog/php-form-validation/ http://www.htmlcenter.com/blog/php-form-validation/

$post = Validate::factory($_POST)

->filter(TRUE, 'trim')
->rule('name','not_empty')
->rule('age','not_empty')

Above code represents how to use it. 上面的代码代表了如何使用它。 For more reference, please visit the following link 有关更多参考,请访问以下链接

http://kohanaframework.org/3.0/guide/kohana/security/validation http://kohanaframework.org/3.0/guide/kohana/security/validation

Hope this helps. 希望这可以帮助。

Thank you. 谢谢。

you can use both javascript frameworks(ie. jquery) and php at the same time. 您可以同时使用javascript框架(即jquery)和php。

your html form posts an ajax data to server. 您的html表单将ajax数据发布到服务器。 your server returns various error or success codes. 您的服务器返回各种错误或成功代码。 then you can manipulate your html using jquery. 那么您可以使用jquery来操作html。 here is a sample on client side usinf jquery; 这是客户端usinf jquery上的示例;

    $( "a#logSubmit").click(function(){
    $.post("/control.php?process=login", {username: $('input#logName').val(), password: $('input#logPass').val()},
   function(data) {
     if(data == "ok"){
     //success
     $( "#dialog-login" ).dialog("close");
     }
     else{
         $("label#input").html("your input is not valid");
         }
   });
});

and your php side should be similar like this 和你的PHP方面应该是这样的

<?php
if(!isset($_POST['nik']) or !isset($_POST['pass']))
{
    die("not ok");
}
if($_POST['nik']=="" or $_POST['pass'] =="")
{

    die("not ok");
}

//here you can`enter code here` validate your data and issue a custom error or success response.

//success!
die('ok');
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM