PHP Page is posting multiple times occasionally

I have a registration form which posts data to save.php. But occasionally the data is getting posted multiple times.

Below is my code for save.php

<?php session_start(); //save registration details in my table  include('connect_database.php'); include('my_functions.php');  $_SESSION['newUser'] = '0'; // new user  //POSTED DATA-------------------------- $t_email = $_POST['email']; $t_psw = $_POST['psw']; $t_first_name = addslashes($_POST['first_name']); $_SESSION['lastname'] = $t_last_name = addslashes($_POST['last_name']); $t_mobile = $_POST['mobile']; $_SESSION['licNum'] = $t_lic_no = $_POST['lic_no']; $t_dob = $_POST['dob']; $t_abn = $_POST['abn']; $tx_expiry = $_POST['tx_expiry']; $drv_for = $_POST['driven_for']; $lng_drv = $_POST['long_driven'];  //referred by $ref_drLic = $_POST['ref_driLic']; $ref_drName = $_POST['ref_driName'];  $t_dr_front = get_image('dr_front',$_POST['last_name'].'_dr_front'); $t_dr_bck = get_image('dr_bck',$_POST['last_name'].'_dr_bck');  //if tx required------- if($_SESSION['ce_cr_tx'] == 1){     $t_tx_front = get_image('tx_front',$_POST['last_name'].'_tx_front');     $t_tx_bck = get_image('tx_bck',$_POST['last_name'].'_tx_bck'); } else{     $t_tx_front = "";     $t_tx_bck = ""; }  //store data in logfile $nwtxt = "Email is - ".$_POST['email'].". Mobile no - ".$_POST['mobile']; writeFile($nwtxt);  //--------------------------------------- //query to save data in my table  $ad_sql = "INSERT INTO myTable (email, password, firstname, lastname, mobile, licence, drfront, drbck, txfront, txbck, cnfrm, dob, abnf, texpiry, drifor, driven, reLic, reNname)     VALUES('".$t_email."','".$t_psw."','".$t_first_name."','".$t_last_name."','".$t_mobile."','".$t_lic_no."','".$t_dr_front."','".$t_dr_bck."','".$t_tx_front."','".$t_tx_bck."','0','".$t_dob."','".$t_abn."','".$tx_expiry."','".$drv_for."','".$lng_drv."','".$ref_drLic."','".$ref_drName."')";  if(!empty($t_email)){     if($conn->query($ad_sql) == true){         //echo'Success';         $lst_id = $conn->insert_id;         $_SESSION['ls_id'] = $lst_id;         $_SESSION['s_email'] = $t_email;         $_SESSION['s_code'] = mt_rand(11111,99999);          //email code to user--------------------------         $subjct = "Email Verification Code";         $usr_msg = "Hi ".$_POST['first_name']." ".$_POST['last_name'].",<br><br>                     A new account has been requested at 'Portal'                     using your email address.<br><br>                      To confirm your new account, please enter this code in the web page:<br>                     <h3>".$_SESSION['s_code']."</h3><br><br>                      If you need help, please call us<br><br>                      Thank you,                     Administrator";         sendEmail($t_email, $usr_msg, $subjct); //sends and email         writeFile('Code is :'.$_SESSION['s_code']); // write a log in file         //--------------------------------------------               //redirect to verify email page----------------------         header("location: verifyEmail.php");         exit();       }     else{         echo'Error creating account-  '.$conn->error.'. Please try again.';         $gbck = "cr=".$_SESSION['ce_cr_id']."&crs=".$_SESSION['ce_cr_nm']."&tx=".$_SESSION['ce_cr_tx']."&erms=Error creating account. Please try again";         header('location: Enroll.php?'.$gbck);         exit();     } } else{     echo'Error creating account. Please try again.';     $gbck = "cr=".$_SESSION['ce_cr_id']."&crs=".$_SESSION['ce_cr_nm']."&tx=".$_SESSION['ce_cr_tx']."&erms= EMPTY data. Error creating account. Please try again";     header('location: Enroll.php?'.$gbck);     exit(); }  ?> 

I checked my code multiple times but couldn’t find anything that is triggering it. When someone registers, the page keeps loading for sometime and I receive multiple entries in database and user receives multiple verification emails.

Is something wrong in my code?

Add Comment
1 Answer(s)

The code itself looks fine, but i get the growing suspicion that it might be a config issue or whats happening before this executes. If your looking for a patchwork fix i would probably put a condition near your if(!empty($t_email)) that checks if the sql table row already exists dont execute, which would rectify the fact that multiple requests are coming in.

Answered on July 16, 2020.
Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.