0

Hi have checked answer from this page: But it uses action="" is it vulnerable to XSS attacks? If yes then without such solution what are my options?

I tried using header redirect. But as I have 2 forms,(in some pages 4-5 forms) header re direction is not working for me with errors.

Here is my code: (Simplified)

1st form: works ok with a redirect.

<form name="ip_block" method="post" class="form-horizontal">
            <div class="form-group">
           <label class="control-label col-sm-2" for="ip"> Enter IP:</label>
            <div class="col-sm-8">
                <input type="text" name="ip" class="form-control" id="ip" />
          </div></div>
         <div class="form-group"> 
            <div class="col-sm-offset-2 col-sm-8">
            <button type="submit" class="btn btn-default" 
            name="ip_block_add">Submit</button>
               </div></div>
             </form> 
        <?php
          if(isset($_POST['ip'])){
              if($IP = filter_input(INPUT_POST, 'ip', 
                FILTER_SANITIZE_STRING)){
              $add_ip = $mysqli->prepare("INSERT INTO block_ip(b_ip) 
                VALUES(?)");
              $add_ip->bind_param("s",$IP);
              $add_ip->execute();
              $add_ip->store_result();
              $add_ip->close();
             header("refresh:5;url=./admin-security.php");// avoiding form 
                 resubmission
             echo 'Added successfully';
              }
              else {
                    echo 'failed to insert';
              }
          }
        ?>

Form 2:

 <form name="clear_data" method="post">
            <input type="hidden" name="data_clear" value="1"/>
            <button type="submit" class="btn btn-warning">Clean Data</button>
        </form>
                 <?php
              if(isset($_POST['data_clear'])){
              if($mysqli->query("CALL clear_old_data")){ 
              header("refresh:5;url=./admin-security.php");// avoiding form resubmission
              echo 'operation successfull'; 
              }   
       else
       {
         echo 'database failure';
          }
        }
      //----
    ?>

For Second form I get error like this

Warning: Cannot modify header information - headers already sent by

For 2nd form I am using header before echo still it doesn't work. reference, I tried with javascript too but that failed.

 echo "<script>setTimeout('window.location.href='./admin-
 security.php';',4000);</script>";

Updated with Dainis Abols idea: but form re submit option is still showing on page refresh

            <form name="clear_data" method="post">
            <input type="hidden" name="data_clear" value="1"/>
            <?php
               $var=111;
               $_SESSION['var']=$var;
               ?>
            <input type="hidden" value="<?php echo $var; ?>" name="varcheck" 
              />
            <button type="submit" class="btn btn-warning">Clean 
                  Data</button>
                   </form>
                 <?php
              if(isset($_POST['data_clear']) && 
            ($_POST['varcheck']==$_SESSION['var'])){
             // Some code
             }
mimi
  • 335
  • 5
  • 25
  • 1
    You can add a token field to your forms that get written in the session. After the submit, just read the submitted token and the one that is stored in your session. That way you can check, if the form has been correctly submitted from your own site. – Peon Sep 13 '17 at 06:38
  • any coding example for this? thank you – mimi Sep 13 '17 at 06:40
  • this operation is done on the admin page, so admin has login session too, hope it will not create any interference. – mimi Sep 13 '17 at 06:42
  • I believe you can write a simple value to variable assignment and session value assignment on your own. – Peon Sep 13 '17 at 06:43
  • Ok testing with your suggestion – mimi Sep 13 '17 at 06:51
  • updated the question with token idea but re submit is still showing – mimi Sep 13 '17 at 07:00
  • If I use `rand()` function instead of 111 then it's not showing the success message. – mimi Sep 13 '17 at 07:02

1 Answers1

1

I'd rather use ajax to send data to the database, without form submiting, and on success I would use js to redirect to /admin-security.php. In this case it's not possible to send the data twice.

Here is the PHP Code:

     <?php
      if(isset($_POST['ip'])){
          if($IP = filter_input(INPUT_POST, 'ip', 
            FILTER_SANITIZE_STRING)){
          $add_ip = $mysqli->prepare("INSERT INTO block_ip(b_ip) 
            VALUES(?)");
          $add_ip->bind_param("s",$IP);
          $add_ip->execute();
          $add_ip->store_result();
          $add_ip->close();
         echo 1;
          }
          else {
                echo 0;
          }
       exit;
      }
    ?>

HTML:

<div class="form-horizontal">
        <div class="form-group">
       <label class="control-label col-sm-2" for="ip"> Enter IP:</label>
        <div class="col-sm-8">
            <input type="text" name="ip" class="form-control" id="ip" />
      </div></div>
     <div class="form-group"> 
        <div class="col-sm-offset-2 col-sm-8">
       <button type="button" onClick="send_form()" class="btn btn-default" 
        >Submit</button>
           </div></div>
         </div>

And AJAX written with JQuery

<script>
function send_form() {
  $.ajax({
     url: "./admin-security.php",
     type: "POST",
     data: {
       ip: $("#ip").val()
     },
     success: function(response) {
          if(response==1) {
              alert("Done");
              location.href = "./admin-security.php";
          }
          else alert("Fail!");
      }
  });
 }
Ylich
  • 60
  • 6
  • plus in some cases, if I need to validate data does it works with your method? – mimi Sep 13 '17 at 06:47
  • Hello, thank you for your code, but it's not working, can you please check your ajax code. I think it's missing a 2nd bracket. Plus location url should be something like this isn't it? Based with my original code `location.href = "./admin-security.php";` – mimi Sep 14 '17 at 05:32
  • Also not sure about this part : `url: "this_page.php",` what will be this page. I have all codes on same page admin-security.php, so will it be something like this `url: "./admin-security.php",` ? – mimi Sep 14 '17 at 05:38
  • @mimi You were right with the second bracket! The url has to be the correct path to your script. If you have the whole script in ./admin-security.php, then you have to put this. I think the code should work now. – Ylich Sep 14 '17 at 06:10
  • thank you for your prompt reply, let me check & get back to you. – mimi Sep 14 '17 at 06:18
  • Its not working still, clicking on the button but the page is not refreshing or any messages are not popping up, I tried with Control + Shift + R hard refresh, but not working still. – mimi Sep 14 '17 at 06:22
  • One question, `onClick="send_form()"`where is this code on button? – mimi Sep 14 '17 at 06:25
  • I'm sorry, I wrote this code so fast I didn't check propperly. Yes, there were not defined the function and there was an extra semi-colon... Now it will work! – Ylich Sep 14 '17 at 06:39
  • No problem at all. But there still an issue on the code, When I enter ip it adds on the database but says "Fail!" not says "Done", for wrong entry its says "Fail!" I understand that, but why for right entries? – mimi Sep 14 '17 at 06:49
  • The if condition in the script is: if($IP = filter_input(INPUT_POST, 'ip', FILTER_SANITIZE_STRING))... You, should rewrite it to check if it wrote the entry into database.... – Ylich Sep 14 '17 at 06:55
  • I am not sure how to implement this, for `if($IP = filter_input(INPUT_POST, 'ip', FILTER_SANITIZE_STRING)){` code already has `echo 1` why should I double check? if echo 1 then jquery should alert as "done" isn't it? – mimi Sep 14 '17 at 07:00
  • For me it's working. Try instead of "Fail!" to put response in the alert box. So you can see whats going on. – Ylich Sep 14 '17 at 07:10
  • Debugging with `else alert(response)` shows all whole html code on alert. – mimi Sep 14 '17 at 07:31
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154408/discussion-between-ylich-and-mimi). – Ylich Sep 14 '17 at 07:32