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 :
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
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>
while true will execute infinite.
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
<?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.