以php格式验证数据 [英] validating data in php form

查看:93
本文介绍了以php格式验证数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想通过Ajax验证php表单的字段,但我必须通过mysql获取该字段的类型,因为我将数据存储在mysql中。所以我得到字段的类型(来自mysql)后我应该用java验证我的数据,或者我可以将它们发送到Mysql以通过Mysql验证,或者我应该用php验证我的数据?

哪个一个是更好,更快,更可靠,更安全吗?

感谢谁帮助但实际上我想验证任何类型甚至原型,我的意思是将来他们可能会更改列的类型我不想通过包括client_side代码在内的整个代码来更改我的字段的验证规则。

我想要更聪明的东西。

提前谢谢。

解决方案

作为一般规则,最好在客户端验证您的数据(并做其他任何事情)。这将消除服务器往返中可能出现的延迟。



这并不总是可行的,例如当需要根据数据库中的数据验证数据时 - 在这种情况下,AJAX可能是比< form>更好的选择。因为它不会擦除客户端上的数据字段。



因为它位于客户端,所以通常使用javaScript。你的提交按钮(或你使用的任何东西)应该通过验证函数,如果验证通过,可以使用formName.submit()数据或调用AJAX。




0。在开始编码之前,您应该已经了解了数据库模式,即表,字段,数据类型及其关系。

1.使用JavaScript执行必要的客户端验证,以满足所需的条目,最小长度,数据类型等,如果发现任何输入无效,将消除往返服务器的往返。请参阅: JavaScript表单验证 [ ^ ]

2.接下来,在服务器端进行必要的安全相关验证,例如帐户验证,清理用户输入等。请参阅 PHP 5表格验证 [ ^ ]。但是,您仍然必须对那些在服务器端通过客户端验证的输入执行验证,因为客户端脚本是明确的源并且打开了黑客攻击。

3.最后,在数据库上部分,可以采取许多措施来确保数据的完整性,例如主键,参照完整性和用户定义的规则。

所有这些措施共同确保应用程序是安全的可靠。

Hi I want to validate fields of a php form by Ajax but I have to get the type of that field by mysql because i store the data in mysql. So I should validate my data with java after I get the type of the field (from mysql) or I can send them to Mysql to be validated by Mysql or I should validate my data with php?
Which one is better, faster, more reliable and more secure?
Thanks to whom helped but actually I want to validate any kind of type even the protoype, I mean maybe in future they change the type of a column I don't want to go through the whole codes including client_side code to change the rule of validation for my field.
I want something more intelligent.
Thanks in advance.

解决方案

As a general rule it is best to validate your data (and do anything else you can) on the client side. This will eliminate possible delays in the server round-trip.

This isn't always possible, such as when the data needs to be validated against that in a database - in which case AJAX is likely a better choice than a <form> as it doesn't scrub the data fields on the client side.

Since it's on the client side, you normally use javaScript for this. Your "submit" button (or whatever you use) should go through the validation function which, if the validation passes, can formName.submit()the data or call AJAX.


0. You should have known of the database schema, i.e. tables, fields, data types, and their relationships before you start coding.
1. Perform the necessary client-side validation using JavaScript for conditions such as required entry, min length, data type, etc. that will eliminate round trip to server if any of the inputs are found to be invalid. Refer: JavaScript Form Validation[^]
2. Next, do the necessary security related validation on the server-side such as account authentication, cleansing of user inputs, etc. See PHP 5 Form Validation[^]. However, you still have to perform validations on those inputs that passes the client-side validation on the server side as client-side script is clear source and opened to hacking.
3. Finally, on the database part, many measures can be put in place to ensure data integrity, such as primary key, referential integrity, and user-defined rules.
All of them work together to make sure that the application is secure and reliable.


这篇关于以php格式验证数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆