internal Login page Infinte loops

I am trying to use the Mikrotik hotspot with following scenario.
When the user connected to hotspot, login.html page redirect to external page index.php to check exist user from csv.
if user exist then it will redirect to redirect.html. if user not exist the it will redirect to register.html & register.php store user info in CSV file.
Whole upper scenario is working fine.
The Main problem is that when redirect.html called, the browser loops between the external server address and the internal server address repeatedly and rapidly.

Here is my login.html Code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>BMC HotSpot</title>
  <meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
    <script type="text/javascript" src="/hotspot/jquery.min.js"></script>
    <script type="text/javascript">
        var js_var = '$(mac)';
        $(document).ready(function () {
            window.location = "http://192.168.88.247/index.php?mac=" + js_var;
    });
    </script>
</head>
<body>
</body>
</html>

Here is my redirect.html code

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>BMC HotSpot</title>
  <meta HTTP-EQUIV="Pragma" CONTENT="no-cache">
  <script type="text/javascript" src="/hotspot/jquery.min.js"></script>
</head>

<body>
$(if chap-id)
	<form name="sendin" action="$(link-login-only)" method="post">
		<input type="hidden" name="username" />
		<input type="hidden" name="password" />
		<input type="hidden" name="dst" value="http://www.dontloseit.com.pk" />
		<input type="hidden" name="popup" value="true" />
	</form>

	<script type="text/javascript" src="/md5.js"></script>
	<script type="text/javascript">
	<!--
	    function doLogin() {
		document.sendin.username.value = document.login.username.value;
		document.sendin.password.value = hexMD5('$(chap-id)' + document.login.password.value + '$(chap-challenge)');
		document.sendin.submit();
		return false;
	    }
	//--></script>
	<script>
	 $(document).ready(function () {
        document.getElementById('test').click();
    });

	</script>
$(endif)

<div align="center">
<a href="$(link-login-only)?target=lv&dst=$(link-orig-esc)" style="">Latviski</a>
</div>

<table width="100%" style="margin-top: 10%; ">
	<tr>
	<td align="center" valign="middle">
		<div class="notice" style="color: #c1c1c1; font-size: 9px;">Please log on to use the internet hotspot service<br />
		<!--$(if trial == 'yes')Free trial available, <a style="color: #FF8080"href="$(link-login-only)?dst=http%3A%2F%2Fwww.gooole.com&username=T-$(mac-esc)">click here</a>.$(endif)--></div><br />
		<table width="280" height="280" style="border: 1px solid #cccccc; padding: 0px;" cellpadding="0" cellspacing="0">
			<tr>
				<td align="center" valign="bottom" height="175" colspan="2">
					<form name="login" action="$(link-login-only)" method="post"
					    $(if chap-id) onSubmit="return doLogin()" $(endif)>
						<input type="hidden" name="dst" value="http://www.dontloseit.com.pk" />
						<input type="hidden" name="popup" value="true" />

							<table width="100" style="background-color: #ffffff">
								<tr><td align="right">login</td>
										<td><input style="width: 80px" name="username" type="text" value="admin"/></td>
								</tr>
								<tr><td align="right">password</td>
										<td>
										<input style="width: 80px" name="password" type="password"/></td>
								</tr>
								<tr><td>&nbsp;</td>
										<td><input type="submit" id="test" value="OK" /></td>
								</tr>
							</table>
					</form>
				</td>
			</tr>
			<tr><td align="center"><a href="http://www.mikrotik.com" target="_blank" style="border: none;"><img src="/img/logobottom.png" alt="mikrotik" /></a></td></tr>
		</table>

	<br /><div style="color: #c1c1c1; font-size: 9px;">Powered by MikroTik RouterOS</div>
	<$(if error)<br /><div style="color: #FF8080; font-size: 9px; ">$(error)</div>$(endif)>
	</td>
	</tr>
</table>

<script type="text/javascript">
<!--
  document.login.username.focus();
//-->
 $(document).ready(function () {
        document.getElementById('test').click();
    });
</script>
</body>
</html>

Here is my index.php code

<?php
$macaddress = $_GET['mac'];
$name = $_GET['name'];

$mac_addrUser = trim($macaddress);
$check = "";

$file = "MacAddressesData.csv";
$file_name=fopen($file,"r");
while (! feof($file_name))
{	
	$data = fgetcsv($file_name);
	if(trim($data[0])==$mac_addrUser)
	{  
	   $check = "true";
	   break;
	}
	   else{
	   $check = "false";
	}
}
fclose($file_name);

   if($check == 'true'){
   header("Location: http://192.168.88.1/redirect.html");
   }
   else{
	   //setcookie("name",$name);
    header("Location: http://192.168.88.1/register.html");
   }
?>

Here is register.php code

<?php
$macaddress = $_GET['mac'];
$mac_addrUser = trim($macaddress);
$name = $_GET['name'];
$email = $_GET['email'];

$file= "MacAddressesData.csv";
$file_name=fopen($file,"r");
while (! feof($file_name))
{	
	$data = fgetcsv($file_name);
	if(trim($data[0])==$mac_addrUser)
	{  
	   $check = "true";   
	   break;
	}
	   else{
	   $check = "false";
	}
}
fclose($file_name);

   if($check == 'true'){
	header("Location: http://192.168.88.1/redirect.html");
   }
   else{
	 $myfile = fopen("MacAddressesData.csv", "a") or die("Unable to open file!");
	 fwrite($myfile, $macaddress. "," . $name . "\n");
	 fclose($myfile);
    header("Location: http://192.168.88.1/redirect.html");
   }

?>

Nobody ? :open_mouth:

Problem Solved !
http://forum.mikrotik.com/t/hotspot-problem/38521/1