简体   繁体   English

使用Ajax每隔X秒运行一个php脚本

[英]Using Ajax to run a php script every X seconds

My project : 我的项目 :

I'm trying to do a captive portal to share the Internet connection but the client have to agree the user agreement first. 我正在尝试使用强制网络门户来共享Internet连接,但客户端必须首先同意用户协议。 After that he can have the Internet connection if he stays on the portal page . 之后, 如果他留在门户页面上 ,他就可以连接互联网。

Issue : 问题 :

My issue is to check if the client is still on my page to provide to him the connection. 我的问题是检查客户端是否仍在我的页面上以向他提供连接。

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). 为此,我尝试每10秒在我的iptables中添加他的MAC地址作为例外,因为我每15秒清除所有MAC异常(除非@MAC超过两次,我让一个)。

My folder contains : 我的文件夹包含:

index.php (the main page where everything is displayed) and mac.php (where I add the exceptions for @MAC) index.php(显示所有内容的主页面)和mac.php(我在其中添加@MAC的例外)

My mac.php : 我的mac.php:

<?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");
?>

I already try to this in my index.php : 我已经在index.php中尝试了这个:

<script>
setInterval(
(function () {
    $("#mac").load("mac.php");
}), 10000);
</script>
<div id="mac"></div>

and this : 和这个 :

    <script>
    setInterval(
    (function () {
        $("#mac").load("mac.php #mac");
    }), 10000);
    </script>

 <div id="mac">
 <?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");
    ?>
</div>

And I already tried other alternatives like : 我已经尝试过其他替代方案,例如:

  • 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 把mac.php的最后两行放在用参数($ macAddr)调用的mac.php中,并用参数调用mac.php

The only way that works from now is to refresh the page every 10s. 从现在开始工作的唯一方法是每10秒刷新一次页面。 But as you may know, it is not the best way... 但是你可能知道,这不是最好的方式......

I'm working an a Linux system. 我正在使用Linux系统。

Thanks for your help 谢谢你的帮助

while true will execute infinite. 而真实将执行无限。
mac.php : mac.php:

    <?php
    function checkMac(){
            $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");


          }
     ?>

all other page where you require to call this page. 您需要调用此页面的所有其他页面。 like ex. 像前。

abc.php abc.php

<?php
require_once('mac.php');
  while(true){
     checkMac();
     sleep(10); // this should halt for 10 seconds for every loop
  } 


?>

Hope this will work for you. 希望这对你有用。

I solved it with : 我解决了它:

<script language="JavaScript">

    function mac() {
        var xmlhttp = new XMLHttpRequest();
        xmlhttp.onreadystatechange = function() {
            xmlhttp.responseText;
        };
        xmlhttp.open("GET", "mac.php", true);
        xmlhttp.send();

    }

    mac();
    setInterval(mac, 10000);
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM