<!DOCTYPE html>
<html>
  <head>
    <link  type='text/css' rel='stylesheet' href='style.css' />
    <title>PHP!</title>
  </head>
  <body>
<?php
header('Content-Type: text/html; charset=unicode'); 
 $file="count.txt";  
       if(!file_exists($file))   
       {   
             exec("echo 0 > $file");   
       }   
      $fp = fopen($file,"r");   
      $num = fgets($fp,6);   
      fclose($fp);   
      print "visited: <br />"."$num"."<br />  times";   
echo '</br>';

$filename = "ip.txt";
function getip_out(){
$ip=false;
if(!empty($_SERVER["HTTP_CLIENT_IP"])){
$ip = $_SERVER["HTTP_CLIENT_IP"];
}
if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
for ($i = 0; $i < count($ips); $i++) {
if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
$ip = $ips[$i];
break;
}
}
}
return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}
$ip = getip_out(); 
echo $ip;
echo '</br>';
//$ip = "98.199.231.217";

// ip138 

    $url="http://www.ip138.com/ips138.asp?ip={$ip}&action=2";
//    $url="http://www.ip.cn/index.php?ip=".$ip;
        $ch = curl_init();
	    $timeout = 5;
	    $this_header = array(
	    "content-type: application/x-www-form-urlencoded; 
	    charset=unicode"
	    );
curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
	        curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
		curl_setopt ($ch, CURLOPT_URL, $url);
		    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
		        curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
			    $str2 = curl_exec($ch);
			    curl_close($ch);
//$match = "/<\/code><\/p><p>(.*)<\/p><\/div><\/div>/" ;
$match = "/<ul class=\"ul1\"><li>(.*)<\/ul><\/td>/" ;
preg_match($match, $str2, $arr1);


echo $str2;
echo $arr1[1];
$pos1=  ( strcmp( $arr1[1] , $arr2[2] ) ? $arr1[1] : "$arr1[1]"."or"."$arr2[1]" );
// sina most detail   <>  http://ip.tee5.com/?123.150.206.207      

$str = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?&ip={$ip}");
preg_match("/-1 -1      (.*)/", $str, $arr2);


$pos=  $arr2[1];

$pos = (!empty($pos)?$pos:'未知');
$pos=  "$pos1"."or"."$pos";
echo $pos;

 if (!$fp = fopen($filename, "r"))
  {
   echo "不能打开文件$";
  exit;
  }
$time =   gmdate("H:i:s",time()+8*3600);
$date =   gmdate("Y-m-d");
$txt=   "$ip"."----"."$date"."-|-"."$time"."----"."$pos"."\r\n";
$check=   "$ip"."----"."$date";
echo '</br>';
echo $txt;

 //写入的时候还判断是否已经有重复数据
 while(!feof($fp))
 {
  $line = fgets($fp);

  if( strpos($line, $check) !== false  )
  { 
   exit;  //有重复数据就退出；
  }
 }
 // 写入文件
      $num+=1;   
$fp = fopen($file,"w"); 
  if(!fwrite($fp,$num))
      {
        echo "不能写入到文件$file" ;
        exit;
         }

 fclose($fp);

$fp = fopen($filename,"a");  
if(!fwrite($fp,$txt))
  {
   echo "不能写入到文件$filename" ;
  exit;
  }
  //已经完成写入文件
  fclose($fp);


 ?> 
</p>
</body>
</html>
