Php prevents refresh and repeated submission of instance code-php Tutorial

Source: Internet
Author: User
Php prevents refresh and repeated submission of instance code

Prevents direct access to PHP pages and can only be referenced! In this way, you cannot directly access page B. You can also add a COOKIE on the "A" and "B" to determine whether to delete the COOKIE after judging the COOKIE. Therefore, you should consider adding a parameter to prevent such a situation. cookies and sessions are available, however, cookies are from the client. if cookies are disabled, they can refresh the clicks maliciously. We recommend that you use the MD5 value of the IP + URL parameter as the SESSION name. set the implementation principle to max_reloadtime = 100; // set the maximum page refresh interval. the user opens the page record for the first time and saves the current time in session_start. the user opens the page for the second time (determines whether session_start exists) the difference between the current time and session_start is time_passed. when time_passed <max_reloadtime indicates that the user refresh the warning frequently within the specified time and then exits directly.

Example:

  1. Session_start ();
  2. $ K = $ _ GET ['K'];
  3. $ T = $ _ GET ['t'];
  4. // Anti-refresh time
  5. $ AllowTime = 1800;
  6. $ Ip = get_client_ip ();
  7. $ AllowT = md5 ($ ip. $ k. $ t );
  8. If (! Isset ($ _ SESSION [$ allowT]) {
  9. $ Refresh = true;
  10. $ _ SESSION [$ allowT] = time ();
  11. } Elseif (time ()-$ _ SESSION [$ allowT]> $ allowTime ){
  12. $ Refresh = true;
  13. $ _ SESSION [$ allowT] = time ();
  14. } Else {
  15. $ Refresh = false;
  16. }
  17. ?>

Example 2: php anti-repeated submission. First, you can define a session variable to save the submission serial number of a form. $ UserLastAction is defined here ".

Then, add a hidden variable to the form and set the value to $ userLastAction + 1:> Finally, determine whether the form has been submitted before processing and submission:

  1. If ($ lastAction> $ userLastAction and inputIsValid (...)) {
  2. $ UserLastAction ++; // add 1 to the serial number
  3. // Process form data
  4. }

Submit page:

  1. $ _ SESSION ['code'] = mt_rand (1000); // Generate a random number between 1 and
  2. ?>

Submitted page:

  1. If ($ _ SESSION ['code']! = $ _ REQUEST ['scode']) {

  2. Echo "Please do not submit again ";
  3. Exit;
  4. }
  5. $ _ SESSION ['code'] = 0

  6. /* Ultimate Edition

  7. PHP prevents users from refreshing pages (Refresh or Reload) and submitting the form content repeatedly.
  8. Because the content of the form variable is referenced by $ _ POST ['name'], you may directly destroy $ _ POST ['name'] (unset () after processing the form ()) you can. Actually not. Because the page caches the form content by default, even if $ _ POST ['name'] is destroyed, after refreshing, $ _ POST ['name'] will still be assigned, which is equally valid.
  9. You can use Session to solve the problem. First, assign a value to the Session, such as 400. after the first successful submission, change the Session value. when the second submission, check the value of this Session. if it is not 400, the data in the form is no longer processed.
  10. Can I set the validity period of a Session?
  11. */
  12. If (isset ($ _ POST ['action']) & $ _ POST ['action'] = 'submitted '){
  13. Session_start ();
  14. Isset ($ _ SESSION ['num']) or die ("no session ");
  15. If ($ _ SESSION ['num'] = 400 ){
  16. Print'
    ’;       
  17. print_r($_POST);
  18. print ‘Please try again’;
  19. print ‘
  20. ';
  21. $ _ SESSION ['num'] = 500;
  22. } Else {
  23. Print'
    ’;       
  24. print_r($_POST);
  25. echo "However you have submitted";
  26. print ‘
  27. ';
  28. }
  29. } Else {
  30. Session_start () or die ("session is not started ");
  31. $ _ SESSION ['num'] = 400;
  32. ?>
  33. }
  34. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.