在PHP中獲取IP來源地址並且插入數據庫

在項目中用到查詢IP來源地址的功能,在網上搜索了一下免費的IP地址數據庫未果,就直接使用ip138網站做爲數據來源了,通過直接使用PHP代碼去查詢IP信息,然後分析網頁來得到地址,最後插入數據庫。 
$content = iconv("gb2312", "utf-8",file_get_contents("http://www.ip138.com/ips8.asp?action=2&ip=$ip"));
var_dump($content);	

			$pos = strpos($content, "<li>本站主數據");
			echo "起始位置$pos</br>";
			if(!$pos){
				hi_logerr("沒有查詢到IP所在地地址!");
				echo "error</br>";
				return false;
			}
			$endPos = stripos($content, "</li>", $pos);
			$jumpLen = strlen("<li>本站主數據:");
			$address = substr($content, $pos + $jumpLen, $endPos - $pos - $jumpLen);

			$sql = "INSERT INTO `ipmap` (`ip` ,`from` ,`updateTime` )VALUES ('$c_ip','$address',CURRENT_TIMESTAMP )";


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章