简体   繁体   English

防止两次执行php脚本

[英]Prevent executing php script twice

maybe this question has been asked. 也许有人问过这个问题。 But cannot find any solution, hence I am posting this myself. 但是找不到任何解决方案,因此我自己发布了此信息。

I need to prevent multiple execution of php script (codeigniter) due to browser hang or network speed. 由于浏览器挂起或网络速度过快,我需要防止多次执行php脚本(codeigniter)。 Because of this it is making multiple entries for single data with datetime difference of 1-2 minutes. 因此,它为单个数据创建了多个条目,日期时间差为1-2分钟。

Stuck around this. 卡在这附近。 Need solution. 需要解决方案。 Any kind of help will be appreciated! 任何帮助将不胜感激!

Simple solution - use form keys. 简单的解决方案-使用表单键。 Each time you render the form, create a random unique string and append it to the form as hidden field. 每次渲染表单时,请创建一个随机的唯一字符串,并将其作为隐藏字段附加到表单中。 In addition to that, store this key inside a table with an timestamp. 除此之外,将此密钥存储在带有时间戳的表中。 You could also associate this entry with the current user if you want. 如果需要,您还可以将该条目与当前用户相关联。

After the user post the form, you just have to check, if the key exists inside the table and delete it. 用户发布表单后,您只需要检查表中是否存在键并将其删除。 If the key don't exists - show some kind of error message. 如果密钥不存在-显示某种错误消息。

The good thing about this approach is also, that you prevent csrf attacks. 这种方法的好处还在于,可以防止csrf攻击。

If you don't want to use an table, you could also store the formkey inside the users session, but doing this would result in additional work, if you want support multiple open browser windows with forms at the same time. 如果您不想使用表,也可以将formkey存储在users会话中,但是如果您想同时支持带有表单的多个打开的浏览器窗口,则这样做会导致额外的工作。

There is also csrf build in, using cookies. 还使用cookie内置了csrf。 You just have to enable it ( $config['csrf_protection'] = TRUE; ) and use form_open to create your form 您只需要启用它( $config['csrf_protection'] = TRUE; ),然后使用form_open创建您的表单

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

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