Teil von  SELFPHP   Teil von  Code Snippets / PHP / HTTP
Professional CronJob-Service

Suche

:: Anbieterverzeichnis ::

Globale Branchen

Informieren Sie sich über ausgewählte Unternehmen im Anbieterverzeichnis von SELFPHP  

 

:: SELFPHP Forum ::

Fragen rund um die Themen PHP?
In über 130.000 Beiträgen finden Sie sicher die passende Antwort!  

 

:: Newsletter ::

Abonnieren Sie hier den kostenlosen SELFPHP Newsletter!

Vorname: 
Name:
E-Mail:
 
 
 
Geo-Daten mit MaxMind zu einer IP-Adresse ermitteln

SELFPHP ist Shopware Solution Partner
Shopware ist ein vielfach ausgezeichnetes Onlineshop-System der shopware AG, das auf PHP. Zend Framework und SQL basiert.
SELFPHP unterstützt Sie als Shopware Solution Partner bei der Konzeption, Programmierung und Realisierung Ihres Onlineshops und passt Shopware bei Bedarf an Ihre Unternehmensbedürfnisse an. Weitere Informationen




Beispielaufgabe

Mit der nachfolgenden PHP5 Klasse stellen wir Ihnen eine Möglichkeit vor, mit der Sie komfortabel weitere Informationen wie z.B. Ort, Latitude, Longitude, ISP oder die Organization basierend von einer gegebenen IP-Adresse ermitteln können. Diese Informationen können hilfreich sein, um zu erfahren woher ein Besucher auf der Webseite kommt. Dabei greift die Klasse auf einen bestehenden Service der Firma MaxMind (www.maxmind.com) zurück, die diese Informationen einmal in einer kostenlosen Variante und einer kostenpflichtigen Variante anbietet. Der Unterschied ist lediglich die Genauigkeit der Daten. Im Nachfolgenden sehen Sie den Zugriff auf die Klasse.




Beschreibung

Bitte achten Sie darauf, dass für den MaxMind-Service eine Registrierung notwendig ist. Im Verlauf der Registrierung erhalten Sie dann Ihre Lizenznummer, der für den Service von Maxmind notwendig ist und den Sie in der Klasse ebenfalls eintragen müssen.
http://www.maxmind.com/app/demo_request?type=city_ws


Die Klasse wurde unter der BSD License gestellt und kann somit für private und kommerzielle Zwecke genutzt werden.



<?PHP

/**
 * 
 * This is maxMindWebservice. Managed MaxMind Web Services GeoIP. 
 * 
 *
 * @package maxMindWebservice 
 * @author Damir Enseleit 
 * @copyright 2010, SELFPHP OHG 
 * @license BSD License 
 * @version 1.0.0 
 * @link http://www.selfphp.de 
 *
 */

class maxMindWebservice
{
    
    private 
$MaxMindlicense "";                // License for MaxMind Web Services
    
private $MaxMindurl         "";                // MaxMind Web Services URL
    
private $MaxMindError     "";                // MaxMind Response-Error
    
private $geoIP                     "";                // IP for Geo-Tracking
    
private $emptyVars            true;            // If empty vars - don't go to Maxmind
    
private $geoData                = array ();    // Store the Geo Data for the IP
    
    // Store all Warnings from maxmind-warnings.txt
    
private $MaxMindWarning                = array ( 'IP_NOT_FOUND','COUNTRY_NOT_FOUND',
    
'CITY_NOT_FOUND','CITY_REQUIRED','POSTAL_CODE_REQUIRED','POSTAL_CODE_NOT_FOUND' );     
        
    
// Store all Fatal-Errors from maxmind-fatalerrors.txt
    
private $MaxMindFatalErrors        = array ( 'INVALID_LICENSE_KEY',
    
'MAX_REQUESTS_PER_LICENSE',    'IP_REQUIRED','LICENSE_REQUIRED',
    
'COUNTRY_REQUIRED','MAX_REQUESTS_REACHED' );     
    
    
// Error Levels
    
private $status = array ( 'OK''ERROR''WARNINGS''FATALERRORS'); 
    private 
$statusLevel 'OK';                // Actual Status-Error-Level
        
        
    /*
     * Constructor 
     */
    
public function __construct$license null$url null ) {
                
        
$this->MaxMindlicense    $license;
        
$this->MaxMindurl    $url;
        
        if ( 
$license != null AND $url != null ) {
            
            
$this->emptyVars    false;
            
        }
        
        
    }
    
    
/**
     * Get the Error Level from MaxMind
     *
     * @return    string          An string with the Errod-Level.
   * 
     */
    
public function getMaxMindErrorStatus () {
        
        return 
$this->MaxMindError;
        
    }
    
    
/**
     * Get the Error Level
     *
     * @return    string          An string with the Errod-Level.
   * 
     */
    
public function getErrorStatus () {
        
        return 
$this->statusLevel;
        
    }
    
    
/**
     * Get the Geo data in a array
     *
     * @return    array          An array with all Geo data.
   * 
     */
    
public function getGeoData ( ) {
        
        return 
$this->geoData;
        
    }
    
    
/**
     * Start GeoIP with the given URL
     *
     * @param      string    $ip        The IP for Geo tracking
     *
     * @return    bool                  True or False
   * 
     */
    
public function startGeoIP $ip null ) {
        
        if ( 
$ip != null AND $this->emptyVars == false ) {
            
            
$this->geoIP    $ip;
            
            
$this->geoData = array ();        
            
            
// Set Errors to null
            
$this->statusLevel $this->status[0];    
            
$this->MaxMindError '';
            
            if ( 
$this->trackingGeoIP () == true ){
                
                return 
true;
                
            }
            else {
                
                return 
false;
                
            }
            
        }
        else {
            
            return 
false;
            
        }
        
    }
    
    
/**
     * Begin the Tracking with an given IP to MaxMind
     *
     * @return    bool                  True or False
   * 
     */
    
private function trackingGeoIP () {
        
        
$query $this->MaxMindurl "?l=" $this->MaxMindlicense "&i=" $this->geoIP;
        
$url parse_url($query);
        
        
$host $url["host"];
        
$path $url["path"] . "?" $url["query"];
        
        
$timeout 1;
        
$fp fsockopen ($host80$errno$errstr$timeout)
            or die(
'Can not open connection to MaxMind-Server.');
        
        if (
$fp) {
          
          
fputs ($fp"GET $path HTTP/1.0\nHost: " $host "\n\n");
          
          while (!
feof($fp)) {
                
$buf .= fgets($fp128);
          }
  
          
$lines split("\n"$buf);
          
$data $lines[count($lines)-1];
          
fclose($fp);
          
          if ( 
$this->scanMaxMindErrors$data ) == true ) {
              
$geo explode(",",$data);
          
              
$this->geoData $geo;
          
              return 
true;
          
          }
          else {
              
              return 
false;
              
          }          
          
        } 
        else {
          
          
# enter error handling code here
          
return false;
        
        }
        
    }
    
    private function 
scanMaxMindErrors $data null) {
        
        
// Scan warnings
        
for ( $i 0$i count($this->MaxMindWarning); $i++ ) {
            if (
eregi($this->MaxMindWarning[$i], $data)) {
            
$this->MaxMindError $this->MaxMindWarning[$i];
            
$this->statusLevel $this->status[2];
            return 
false;
            }
        }
        
        
// Scan Fatal-Errors
        
for ( $i 0$i count($this->MaxMindFatalErrors); $i++ ) {
            if (
eregi($this->MaxMindFatalErrors[$i], $data)) {
            
$this->MaxMindError $this->MaxMindFatalErrors[$i];
            
$this->statusLevel $this->status[3];
            return 
false;
            }
        }
        
        return 
true;
        
    }
        
}

?>



Anwendungsbeispiel

<?PHP

include ("maxmind-webservice.inc.php");

$maxmind = new maxMindWebservice("Ihre Lizenz","http://geoip1.maxmind.com/f");

$maxmind->startGeoIP("80.144.87.13"); 

$data $maxmind->getGeoData();

print_r$data );

?>



Ausgabebeispiel: Browseransicht

Array
(
[0] => DE
[1] => 07
[2] => Witten
[3] =>
[4] => 51.433300
[5] => 7.333300
[6] => 0
[7] => 0
[8] => "Deutsche Telekom AG"
[9] => "Deutsche Telekom AG"
)








 




:: Premium-Partner ::

Webhosting/Serverlösungen

Pixel X weitere Premium-Partner
 

:: SELFPHP Sponsor ::

Kaspersky Labs GmbH weitere Sponsoren
 


:: Buchempfehlung ::

PHP 5.3 & MySQL 5.1

PHP 5.3 & MySQL 5.1 zur Buchempfehlung
 

:: Anbieterverzeichnis ::

Webhosting/Serverlösungen

Suchen Sie den für Sie passenden IT-Dienstleister für Ihr Webhosting-Paket oder Ihre Serverlösung?

Sie sind nur ein paar Klicks davon entfernt!


 

Ausgewählter Tipp im Bereich PHP-Skripte


Schaltjahre bestimmen

Weitere interessante Beispiele aus dem SELFPHP Kochbuch finden Sie im Bereich PHP-Skripte
 

SELFPHP Code Snippet


Ersetzen von nicht erlaubten oder gewünschten Zeichen im Dateinamen

Weitere interessante Code Snippets finden Sie auf SELFPHP im Bereich PHP Code Snippets
 
© 2001-2024 E-Mail SELFPHP Inh. Damir Enseleit, info@selfphp.deImpressumKontakt