安全Ajax表单POST [英] Secure ajax form POST

查看:94
本文介绍了安全Ajax表单POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何开发通过AJAX安全的表单提交。

I was wondering how to develop a secure form post through AJAX.

例如,我有:

我的HTML表单。

我的JavaScript处理提交。

My JavaScript handling the submit.

在提交网址是post_data.php

The submit url is "post_data.php"

在公布的数据是:

ID = 8&安培;名称=丹尼斯

id=8&name=Denis

PHP的验证,如果变量ID和姓名张贴和他们的数据类型。如果这是确定它着手做一些东西上的数据库。

The PHP verifies if variables id and name are POSTED and their data type. If this is ok it proceed to do some stuff on a database.

我的问题是,我怎么能prevent有人从自己创造的HTML表单,超出了我的网站,或什么的,并张贴虚假数据,以我的PHP脚本?

My question is, how can i prevent someone from creating his own html form, outside my web site, or whatever, and posting false data to my PHP script?

想象一下,数据真的存在于我的数据库,这可能是坏的。

Imagine that data realy exists on my database, this could be bad.

感谢

推荐答案

这样做的一种很常见的方式是有某种列入的令牌的<隐藏> 字段表单上,同样一个保存在session变量中(或其他地方),您的服务器上。当后提交,您检查令牌是有效的。

One very common way to do this is to have a token of some kind included in a <hidden> field on your form, and the same one saved in a session variable (or somewhere else) on your server. When the post is submitted, you check that the token is valid.

其他人仍然可以伪造一个道理,但他们不能(在任何简单的方法,至少)强迫你救了同样的服务器上,所以没有其他形式的比你自​​己会被接受。

Someone else could still forge a token, but they can't (in any easy way, at least) force you to save the same token on your server, so no other form than your own will be accepted.

这是,例如,如何为这个在ASP.NET MVC内置的支持工作。

This is, for example, how the built-in support for this in ASP.NET MVC works.

这篇关于安全Ajax表单POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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