title: Using Ajax to run a php script every X seconds
body: <p><strong>My project :</strong></p>
  
  <p>I'm trying to do a captive portal to share the Internet connection but the client have to agree the user agreement first. After that he can have the Internet connection <strong>if he stays on the portal page</strong>.</p>
  
  <p><strong>Issue :</strong></p>
  
  <p>My issue is to check if the client is still on my page to provide to him the connection.</p>
  
  <p>For this, I try to add his MAC address as an exception in my iptables every 10 sec because I flush all MAC exception every 15s (except if the @MAC is more than twice, I let one).</p>
  
  <p><strong>My folder contains :</strong></p>
  
  <p>index.php (the main page where everything is displayed) and mac.php (where I add the exceptions for @MAC)</p>
  
  <p><strong>My mac.php :</strong></p>
  
  <pre><code>&lt;?php
  $ipAddress=$_SERVER['REMOTE_ADDR'];
  
  $macAddr=false;
  
  $arp=`arp -a $ipAddress`;
  $lines=explode(" ", $arp);
  $macAddr=$lines[3];
  
  echo exec("sudo /sbin/iptables -t nat -I PREROUTING -m mac --mac-source $macAddr -j ACCEPT");
  echo exec("sudo /sbin/iptables -I FORWARD -m mac --mac-source $macAddr -j ACCEPT");
  ?&gt;
  </code></pre>
  
  <p><strong>I already try to this in my index.php :</strong></p>
  
  <pre><code>&lt;script&gt;
  setInterval(
  (function () {
      $("#mac").load("mac.php");
  }), 10000);
  &lt;/script&gt;
  &lt;div id="mac"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>and this :</p>
  
  <pre><code>    &lt;script&gt;
      setInterval(
      (function () {
          $("#mac").load("mac.php #mac");
      }), 10000);
      &lt;/script&gt;
  
   &lt;div id="mac"&gt;
   &lt;?php
      $ipAddress=$_SERVER['REMOTE_ADDR'];
  
      $macAddr=false;
  
      $arp=`arp -a $ipAddress`;
      $lines=explode(" ", $arp);
      $macAddr=$lines[3];
  
      echo exec("sudo /sbin/iptables -t nat -I PREROUTING -m mac --mac-source $macAddr -j ACCEPT");
      echo exec("sudo /sbin/iptables -I FORWARD -m mac --mac-source $macAddr -j ACCEPT");
      ?&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>And I already tried other alternatives like :</p>
  
  <ul>
  <li>to put the the 2 last lines of mac.php in mac.php called with an argument ($macAddr) and to call mac.php with an argument</li>
  </ul>
  
  <p>The only way that works from now is to refresh the page every 10s. But as you may know, it is not the best way...</p>
  
  <p>I'm working an a Linux system.</p>
  
  <p>Thanks for your help </p>