douqun1977 2015-11-19 16:51
浏览 67
已采纳

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

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. 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).

My folder contains :

index.php (the main page where everything is displayed) and mac.php (where I add the exceptions for @MAC)

My 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 :

<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

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...

I'm working an a Linux system.

Thanks for your help

  • 写回答

2条回答 默认 最新

  • doudang8824 2015-11-20 09:29
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看