/** * @return array|bool false if city is not detected */ public function getCity() { if (!$this->SxGeo) { $this->loadSxGeo(); } return $this->SxGeo->getCity($this->getUserIp()); }
/** * @param string $ip * * @return array|bool */ public function getCity($ip = '') { return $this->sxGeo->getCity($this->getIp($ip)); }
/** * @return array|bool false if city is not detected */ public function getCity() { return $this->SxGeo->getCity($this->getUserIp()); }
<?php // Если страна не определилась $COUNTRY_DEFAULT = 'ua'; $CITY_DEFAULT = 'kharkiv'; // Детект страны и города по IP // Docs: https://sypexgeo.net/ru/docs/ include 'inc/vendor/SxGeo.php'; $SxGeo = new SxGeo('inc/vendor/SxGeoCity.dat'); $geo = $SxGeo->getCity($_SERVER['REMOTE_ADDR']); $country = $geo ? strtolower($geo['country']['iso']) : $COUNTRY_DEFAULT; $city = $geo ? strtolower($geo['city']['name_en']) : $CITY_DEFAULT; ?> <script> // Detect user's preferred language (from browser & OS settings) // More info: https://alicoding.com/detect-browser-language-preference-in-firefox-and-chrome-using-javascript/ var langPreferred = window.navigator.languages ? window.navigator.languages[0] : null; langPreferred = langPreferred || window.navigator.language || window.navigator.browserLanguage || window.navigator.userLanguage; if (langPreferred.indexOf('-') !== -1) langPreferred = langPreferred.split('-')[0]; if (langPreferred.indexOf('_') !== -1) langPreferred = langPreferred.split('_')[0]; $('body').addClass('-langPreferred_'+langPreferred); </script> <?php /* Если вам нужно, можете сделать редирект по условию: if ($country == 'ua') {
/** * Returns city info array for given IP * * Sample array: * ( * [regid] => 9737 * [cc] => 185 * [fips] => 66 * [lat] => 60.076238 * [lon] => 30.121382 * [country] => RU * [city] => Санкт-Петербург * ) * * @param string $ip IP address in xxx.xxx.xxx.xxx notation * @return Ambigous <string> * */ function sx_getCityInfo($ip = null) { global $SxCity, $sx_bulkmode, $sx_city, $sx_ip; if (is_null($ip)) { $ip = $sx_ip; } if (!$SxCity) { $SxCity = new SxGeo('plugins/sxgeo/data/SxGeoCity.dat'); } // Режим по умолчанию, файл бд SxGeo.dat $sx_city['info'] = $result = @$SxCity->getCity($ip); // возвращает с краткой информацией, без названия региона и временной зоны if (!$sx_bulkmode) { unset($SxCity); } // Если нужно освободить ресурсы - удаляем объект return $result; }