PHP prevents refresh and repeated submission of example code

WBOY
Release: 2016-07-25 09:12:19
Original
1180 people have browsed it

Prevent direct access to the PHP page, only quote it! In this way, you cannot directly access the B page. You can also add COOKIE to A and delete the COOKIE after B determines the COOKIE. Prevent module refresh So I considered adding a parameter to prevent this kind of situation from happening. COOKIE and SESSION are available, but COOKIE is client-side. If someone disables COOKIE, they can still maliciously refresh the number of clicks. It is better to use SESSION. The MD5 value of IP+URL parameters is used as the SESSION name. I think it is difficult for everyone to forge it. Implementation principle: Set max_reloadtime =100; //Set the maximum page refresh interval The first time the user opens the page, the current time is recorded and saved in session_start The second time the user opens the page (to determine whether session_start exists) subtracts the current time from session_start to get the difference time_passed When time_passed

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. ?>
Copy code

Example 2, PHP prevents duplicate submission. First, you can define a session variable to save the submission sequence number of a form. This is defined as "$userLastAction".

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 the submission:

  1. if($lastAction>$userLastAction and inputIsValid(…)){
  2. $userLastAction++; // Add 1 to the sequence number
  3. // Process form data
  4. }
Copy code

Submit page:

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

Submitted page:

  1. if($_SESSION['code']!=$_REQUEST['scode']){

  2. echo "Please do not resubmit";
  3. exit;
  4. }
  5. $_SESSION[' code']=0

  6. /* Improved version

  7. PHP prevents users from refreshing the page (Refresh or Reload) and submitting form content repeatedly.
  8. Since the content of the form variable is referenced by $_POST['name'], maybe after processing the form, $_POST['name'] can be destroyed directly (unset()). actually not. It may be that the page caches the form content by default, so even if $_POST['name'] is destroyed, $_POST['name'] will still be assigned a value after refreshing, and it is still valid.
  9. Can be solved using Session. First assign a value to the Session, such as 400. After the first submission is successful, change the value of the Session. When submitting the second time, check the value of the Session. If it is not 400, the data in the form will no longer be processed.
  10. Can the validity time of Session be set?
  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 '
    '; </li>
    <li> print_r($_POST); </li>
    <li> print '<a href="'. $_SERVER ['PHP_SELF'] .'">Please try again</a>'; </li>
    <li> print '
    ';
  17. $_SESSION['num']=500;
  18. } else {
  19. print ' <li> print_r($_POST); </li> <li> echo "However you have submitted"; </li> <li> print '';
  20. }
  21. } else {
  22. session_start() or die("session is not started");
  23. $_SESSION['num']= 400;
  24. ?>
  25. Name: < ;input type="text" name="personal[name]">
  26. Email:
  27. Beer: < ;br>

  28. < input type="submit" name="submit" value="submit me!">
  29. }
  30. ?>

Copy code

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!