简体   繁体   English

使用Ajax + JQuery + MySql + PHP保存表单

[英]Save form as progressed with Ajax + JQuery + MySql + PHP

I was wondering about the best way to setup to following. 我想知道设置跟随的最佳方式。

I have a dynamic form that can potentially get very long once a user has finished adding their data. 我有一个动态表单,一旦用户完成添加他们的数据,可能会很长。 What I was hoping to setup is a background process that saves the data they have to the database. 我希望设置的是一个后台进程,它将数据保存到数据库中。

What I had setup was a settimer function that would check if a question is in a saved state and if not add it to a list of questions to save. 我设置的是一个settimer函数,用于检查问题是否处于已保存状态,如果没有,则将其添加到要保存的问题列表中。 Then it would trigger an ajax call which uses php to post to my database. 然后它会触发一个使用php发布到我的数据库的ajax调用。 The first time this happens the ajax will return the id of the submission so the form knows where to post these to. 第一次发生这种情况时,ajax将返回提交的id,以便表单知道将这些内容发布到哪里。

The second time the set timer function is run it passes this id. 第二次运行set timer函数时,它会传递此id。

My biggest problem is I don't want the user to know table ids. 我最大的问题是我不希望用户知道表ID。 I was just thinking someone can mess with the javascript and submit their results to other ids pretty easily. 我只是觉得有人可以搞乱javascript并很容易将结果提交给其他ID。

I was just wondering if there is a better way to achieve this? 我只是想知道是否有更好的方法来实现这一目标? Or even some examples? 甚至是一些例子?

Thanks 谢谢

Generally speaking, you should probably keep the ID on the server-side in a $_SESSION variable and never let it get anywhere near the client. 一般来说,您应该将服务器端的ID保存在$_SESSION变量中,并且永远不要让它靠近客户端。 Alternatively, the form might contain sufficiently identifiable information to be able to determine the relevant record on the basis of some UNIQUE index. 或者,表单可能包含足够可识别的信息,以便能够根据某些UNIQUE索引确定相关记录。

However, if you prefer, you could also add some cryptographic authentication to prove that the ID has not been altered by the user. 但是,如果您愿意,还可以添加一些加密身份验证,以证明该ID未被用户更改。 For example, in addition to the ID itself, return to the client a secure hash of the ID concatenated with some secret; 例如,除了ID本身之外,还要向客户端返回与某个秘密连接的ID的安全散列; when the client posts back to your server both the ID and that hash, you can compare against a recalculated version of the hash to check that only someone who knew the secret (ie you) could have provided the client with that ID. 当客户端将ID和该哈希值都回发到您的服务器时,您可以与重新计算的哈希值进行比较,以检查只有知道该密码的人(即您)才能为客户端提供该ID。

In order to defeat more sophisticated attacks, you might want to add some salt: 为了打败更复杂的攻击,你可能想添加一些盐:

$salt = some_random_string();
$hash = md5( md5($secret) . md5($id) . md5($salt) )

Then send to the client the hash, the ID and the salt; 然后向客户端发送哈希,ID 盐; all three of which it will return to you for validation. 所有这三个都将返回给您进行验证。

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

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