<?php
//ob_start();
@ ini_set('display_errors', false);
@ error_reporting(E_ERROR | E_WARNING | E_PARSE);
//header("content-type: text/html; charset=gb2312");

if (!isset($_COOKIE['city']) || empty($_COOKIE['city'])) {
  require_once('inc/city/getip.php');
  $myobj = new ipLocation();
  $ip = $myobj->getIP();
  $address = $myobj->getaddress($ip);
  $myobj = NULL;
  $city = getCITY($address["area1"]);
} else {
  $city = array('province' => @$_COOKIE['province'], 'city' => $_COOKIE['city']);
}


//得到城市
function getCITY($city) {
  global $city_sql;
  if (!empty($city)) {
    $province = preg_replace('/^.*(新疆|广西|内蒙古|宁夏|西藏)(.*自治区)?.*$/', '$1', $city);
    $province = preg_replace('/^.*(台湾|广东|江苏|浙江|山东|福建|辽宁|黑龙江|河北|湖北|湖南|安徽|吉林|海南|四川|陕西|山西|河南|云南|甘肃|江西|青海|贵州)(省)?.*$/', '$1', $province);
    $province = @iconv("gb2312", "utf-8", $province);
    $city = preg_replace('/^.*(新疆|广西|内蒙古|宁夏|西藏)(.*自治区)?/', '', $city);
    $city = preg_replace('/^.*(台湾|广东|江苏|浙江|山东|福建|辽宁|黑龙江|河北|湖北|湖南|安徽|吉林|海南|四川|陕西|山西|河南|云南|甘肃|江西|青海|贵州)(.*省)?/', '', $city);
    $city = @iconv("gb2312", "utf-8", $city);
    setcookie('city', $city, time() + 365 * 24 * 60 * 60, '/');
    setcookie('province', $province, time() + 365 * 24 * 60 * 60, '/');
    return array('province' => $province, 'city' => $city);
  }
  return false;
}

















?>