1

I fallow to this guide:

https://stackoverflow.com/a/34185462/20508390

First I install via composer this library:

https://github.com/maxmind/GeoIP2-php

Now I see vendor folder in codeigniter

public_html/vendor/maxmind
public_html/vendor/maxmind-db
public_html/vendor/geoip2

Fallowed to documentation I copy all files from: public_html/vendor/geoip2/geoip2/src/

to

public_html/application/third_party/GeoIp2/vendor/GeoIp2/

here I put folders and files from src directory :

Database Exception Model Record WebService ProviderInterface.php Util.php

Now inside /application/libraries I created a file called CI_GeoIp2.php and add the following code.

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * GeoIp2 Class
 *
 * @package       CodeIgniter
 * @subpackage  Libraries
 * @category      GeoIp2
 * @author        Timothy Marois <timothymarois@gmail.com>
 */
require_once( APPPATH . 'third_party/GeoIp2/vendor/autoload.php' );
use GeoIp2\Database\Reader;

class CI_GeoIp2 { 

    protected $record;
    protected $database_path = 'third_party/GeoIp2/GeoIP2-City.mmdb';

    public function __construct() {

        $ci =& get_instance();
        $reader = new Reader(APPPATH.$this->database_path);

        $ip = $ci->input->ip_address();
        if ($ci->input->valid_ip($ip)) {
            $this->record = $reader->city($ip);
        }

        log_message('debug', "CI_GeoIp2 Class Initialized");
    }


    /**
     * getState()
     * @return state
     */
    public function getState() {
        return $this->record->mostSpecificSubdivision->name;;
    }


    /**
     * getState()
     * @return country code "US/CA etc"
     */
    public function getCountryCode() {
        return $this->record->country->isoCode;
    }


    /**
     * getCity()
     * @return city name
     */
    public function getCity() {
        return $this->record->city->name;
    }


    /**
     * getZipCode()
     * @return Zip Code (#)
     */
    public function getZipCode() {
        return $this->record->postal->code;
    }


    /**
     * getRawRecord()
     * (if you want to manually extract objects)
     *
     * @return object of all items
     */
    public function getRawRecord() {
        return $this->record;
    }

}

Then I get error:

application/third_party/GeoIp2/vendor/autoload.php): failed to open stream: No such file or directory

The problem is I cannot locate any autoload.php in public_html/vendor/geoip2 I not see this file. I checked other folders and I found autoload.php here public_html/vendor/maxmind-db

So I copy this autoload.php and paste in:

public_html/application/third_party/GeoIp2/vendor/autoload.php

After refresh page now I see issue:

Message: Class 'GeoIp2\Database\Reader' not found

I checked and I see:

public_html/application/third_party/GeoIp2/vendor/GeoIp2/Database/Reader.php

also I copy public_html/vendor/maxmind-db content from src. to

applicaiton/third_party/GeoIp2/vendor/MaxMind and this still not resolve issue.

For summary, currently I have this structure:

/application
├── third_party
│   ├── GeoIp2
│   │   ├── vendor
│   │   ├── composer.json
│   │   │ ├── GeoIp2/Database Exception Model Record WebService ProviderInterface.php Util.php
│   │   │ ├── MaxMind/DB/Reader  Reader.php
│   │   │ ├── autoload.php

1 Answers1

1

You should install GeoIP2 package:

composer require geoip2/geoip2