简体   繁体   中英

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. Because of this it is making multiple entries for single data with datetime difference of 1-2 minutes.

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.

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.

There is also csrf build in, using cookies. You just have to enable it ( $config['csrf_protection'] = TRUE; ) and use form_open to create your form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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