<?php
	$n=0;
	$tmptime=time()-120;		//两分钟以前
	$db=mysql_pconnect(get_cfg_var("SIPSERVER"),get_cfg_var("SIPUSER"),get_cfg_var("SIPPWD"));
	mysql_select_db(get_cfg_var("SIPDB"),$db);
	$rs = mysql_query("select ipaddr from contact where contacttime<=".strval($tmptime),$db);	//读取最近一次定时联络时间在2分钟以前的、联络超时的记录
	while ($row = mysql_fetch_array($rs)) {		//把每个IP插入logout表，通知计费器关闭连接
		$userIP = $row["ipaddr"];
		$rs1 = mysql_query("select ipaddr from logout where ipaddr='".$userIP."'",$db);		//查找该IP是否已经存在
		if ( ($row1 = mysql_fetch_array($rs1))==FALSE ) {			//还没有存在
			mysql_free_result($rs1);
			//向logout表中插入该上网IP地址，以通知监控计费器取消该IP地址连接
			mysql_query( "insert into logout(ipaddr) values('".$userIP."')",$db );
			$deleteIP[$n]=$userIP;
			$n=$n+1;
		}
		else {
			mysql_free_result($rs1);
		}
	}
	mysql_free_result($rs);
	for ($i=0;$i<$n;$i++) {
		$userIP=$deleteIP[$i];
		mysql_query("delete from contact where ipaddr='".$userIP."'",$db);
	}
	mysql_close($db);			//关闭数据库连接
?>
<p>定时联络完成！</p>
