Esempio n. 1
0
 /**
  * Returns the GeoIp Location for the given IP address.
  *
  * @param null|string $ip Address for which to return the location data.
  *                        Note, if you leave it empty, the location will be determined using the current client IP.
  *
  * @return Location
  * @throws GeoIpException
  * @throws \Exception
  */
 public function getGeoIpLocation($ip = null)
 {
     if (empty($ip)) {
         $ip = $this->getCurrentIp();
     }
     // check if we have it in session
     $sessionKey = $this->getSessionKey($ip);
     $geoData = $this->httpSession()->get($sessionKey, false);
     if ($geoData) {
         $location = new Location();
         $location->populate($geoData);
         return $location;
     }
     $protocol = $this->getIpProtocol($ip);
     try {
         if ($protocol == 4) {
             $location = $this->provider->getLocationFromIPv4($ip);
         } else {
             $location = $this->provider->getLocationFromIPv6($ip);
         }
         // save it into session
         $this->httpSession()->save($sessionKey, $location->exportToJson());
         return $location;
     } catch (ProviderGeoIpNotFound $e) {
         return false;
     } catch (\Exception $e) {
         throw $e;
     }
 }