Yii 2 Ip Info

Latest Stable Version Latest Unstable Version Total Downloads Monthly Downloads Daily Downloads
Thankful to Krajee!
to get more out of us.

NOTE: This extension depends on the kartik-v/yii2-krajee-base extension which in turn depends on the yiisoft/yii2-bootstrap extension. Check the composer.json for this extension's requirements and dependencies that may be updated by composer.

An IP address information display widget for Yii framework 2.0 with ability to display country flag and geo position info. This is based on the HTTP API from freegeoip.net to parse IP address details. The plugin also uses the yii2-popover-x extension by Krajee for displaying details of the IP in a popover and the yii2-icons extension by Krajee for displaying the flag icons. The widget offers these various features:
  • Ability to display the flag for a IP address.

  • Ability to display geo position details for the IP address.

  • Ability to render IP details inline instead of popover.

  • Ability to configure fields rendered and also control the layout with templates.

  • Use yii2-popover-x extension features to control popover placements and styles.

  • Use flag icons from yii2-icons to render country wise high resolution flags of any size.

  • Uses Yii i18N translations to generate locale specific data.

The preferred way to install this extension is through composer. Either run:

$ php composer.phar require kartik-v/yii2-ipinfo "@dev"

or add:

"kartik-v/yii2-ipinfo": "@dev"

to the require section of your composer.json file. Then run:

php composer.phar update

to get the updated package on your application install.

The IpInfo widget supports the following properties:
  • api: string, the api used for fetching the IP information in json format. Defaults to http://freegeoip.net/json/. There is a limitation on number of queries allowed per hour from this site. To have an unlimited access, you can choose to download the entire geo IP database and code from http://freegeoip.net and install on your web server. Then set this api property to point to your server's URL.

  • ip: string, the ip address. If this is not set, the current session's ip address will be used.

  • template: array, the template configuration for rendering the popover button, popover content, or inline content. This should be set as $key => $value pairs, where $key is one of:

    • popoverButton: this is the template for popover button label (applied when showPopover is true)

    • popoverContent: this is the template for popover content displayed on click of the button (applied when showPopover is true).

    • inlineContent: this is the template for inline content when showPopover is set to false.

    The $value is the template setting and can contain tags in braces, which will represent value of each IP position field (set in fields property) fetched from the freegeoip.net API (for example {country_code}, {country_name} etc.). The following additional special tags will be replaced:

    • {flag}: Will be replaced with the flag icon rendered based on the showFlag setting.

    • {table}: Will render all fields configured via fields in a tabular format of labels and values.

  • showFlag: boolean, whether to display the country flag by parsing the ip address. Defaults to true.

  • showPopover: boolean, whether to show position details in a popover on click of the flag. If set to false, the results will be rendered inline. Defaults to true. This will use the yii2-popover-x extension by Krajee for rendering the popover.

  • defaultFieldValues: array, the default initial values for the field tags used in the template property before they are fetched from the API. Defaults to:

    [
        'flag' => '<i class="glyphicon glyphicon-question-sign text-warning"></i>',
        'country_code' => 'N.A.'
        'country_name' => 'Unknown'
    ]
    
  • noData: string, the message to be shown when no data is found. Defaults to:

    No data found for IP address {ip}
  • noDataOptions: array, the HTML attributes for the no data text. Defaults to:

    ['class' => 'alert alert-danger text-center']

    The following special tags are recognized:

    • tag: string, the tag in which content will be rendered. Defaults to div.

  • errorData: string, the markup to be displayed when any exception is faced during processing by the API (e.g. no connectivity). You can set this to a blank string to not display anything. Defaults to:

    <i class="glyphicon glyphicon-exclamation-sign text-danger"></i>
  • errorDataOptions: array, the HTML attributes for error data container. Defaults to:

    ['title' => 'IP fetch error']

    The following special tags are recognized:

    • tag: string, the tag in which content will be rendered. Defaults to span.

  • fields: array, array the column fields to be display as details. Each item in this array must correspond to the field key for each record in the JSON output. Note that the fields will be displayed in the same order as you set it here. For example you can set this to ['country_name', 'city'] to just display the country name and city. If empty or not set, all fields will be displayed. The list of all fields from API JSON and their translated names are:

     [
        'ip' => Yii::t('kvip', 'IP Address'),
        'country_code' => Yii::t('kvip', 'Country Code'),
        'country_name' => Yii::t('kvip', 'Country Name'),
        'region_code' => Yii::t('kvip', 'Region Code'),
        'region_name' => Yii::t('kvip', 'Region Name'),
        'city' => Yii::t('kvip', 'City'),
        'zip_code' => Yii::t('kvip', 'Zip Code'),
        'time_zone' => Yii::t('kvip', 'Time Zone'),
        'latitude' => Yii::t('kvip', 'Latitude'),
        'longitude' => Yii::t('kvip', 'Longitude'),
        'metro_code' => Yii::t('kvip', 'Metro Code')
    ]
     
  • popoverOptions: array, the widget settings for the popover. Refer the yii2-popover-x extension documentation for details of settings. Defaults to:

    [
        'toggleButton' => ['class' => 'btn btn-xs btn-link', 'style' => 'margin:0']
    ]
     
  • loadingOptions: array, the HTML attributes for the loading container that will be shown while the IP information is being fetched via AJAX. Defaults to ['class' => 'kv-ip-loading']. The following special tags are recognized:

    • tag: string, the tag in which content will be rendered. Defaults to div.

    • message: string, the message to display while loading. Defaults to Fetching location info....

  • flagWrapperOptions: array, the HTML attributes for the flag wrapper container.

  • flagOptions: array, the HTML attributes for the flag image (rendered via flag-icon-css in kartik-v/yii2-icons).

  • contentHeader: string, the icon shown before the header title for content in the popover. Defaults to:

    <i class="glyphicon glyphicon-map-marker"></i> 
  • contentOptions: string, the HTML attributes for the ip info content table container. Defaults to:

    ['class' => 'table']

  • options: array, the HTML attributes for the overall widget container. The following special tags are recognized:

    • tag: string, the tag in which content will be rendered. Defaults to span.

Basic default usage without any options. When ip is not set, the IP address of the user session will be used to determine the IP info. Click the flag icon to get details.

Show details for a specific ip address and style your popover toggle button. Display details in a popover on clicking the flag icon. Control popover styles and layout by setting popoverOptions.

Expand example above to configure your own template for displaying additional field content in the toggle button

Fetching location info...
Hide popover and display the results inline. Set your table CSS styles if needed.

Fetching location info...
Display a full size flag for the same example above.

You are logged in from
Fetching location info...
Hide flag and display results inline. Configure fields to reorder fields and only show the column fields you need to display.

Fetching location info...
N.A.
Configure template for inline layout without popover (for example show flag, city, and country_code)
use kartik\ipinfo\IpInfo;
use kartik\popover\PopoverX;

// Basic default usage without any options. When <code>ip</code> is not set, the IP address of 
// the user session will be used to determine the IP info. Click the flag icon to get details.
echo IpInfo::widget();

// Show details for a specific ip address and style your popover toggle button. Display details
// in a popover on clicking the flag icon. Control popover styles and layout by setting 
// `popoverOptions`.
echo IpInfo::widget([
    'ip' => '12.23.155.123',
    'flagWrapperOptions' => [
        'class' => 'btn btn-lg btn-default'
    ],
    'popoverOptions' => [
        'placement' => PopoverX::ALIGN_BOTTOM_LEFT
    ]
]);

// Expand example above to configure your own template for displaying additional field content
// in the toggle button
echo IpInfo::widget([
    'ip' => '12.23.155.123',
    'template' => '{flag} {country_code} ({city} {region_name} {country_name} {zip_code})',
    'popoverOptions' => [
        'placement' => PopoverX::ALIGN_BOTTOM_LEFT,
        'size' => PopoverX::SIZE_MEDIUM
    ]
]);

// Hide popover and display the results inline. Set your table CSS styles if needed.
echo IpInfo::widget([
    'showPopover' => false,  
    'contentOptions' => [
        'class' => 'table table-bordered table-striped'
    ],
    'flagWrapperOptions' => ['class' => 'kv-flag-center', 'style'=>'width:100px;height:75px;'],
    'flagOptions' => ['class' => 'kv-flag-bordered flag-icon']
]);

// Display a full size flag for the same example above
echo IpInfo::widget([
    'showPopover' => false,  
    'contentOptions' => [
        'class' => 'table table-bordered table-striped'
    ],
    'flagWrapperOptions' => ['class' => 'kv-flag-wrapper'],
    'flagOptions' => ['class' => 'img-thumbnail flag flag-icon-background']
]);

// Hide flag and display results inline. Configure <code>fields</code> to reorder fields and only
// show the column fields you need to display.
echo IpInfo::widget([
    'showFlag' => false,
    'fields' => ['country_name', 'city']
]);

// Configure template for inline layout without popover (for example show
// `flag`, `city`, and `country_code`)
echo IpInfo::widget([
    'ip' => '12.23.155.123',
    'showPopover' => false,
    'template' => ['inlineContent'=>'{flag} {city} {country_code} '],
    'popoverOptions' => [
        'placement' => PopoverX::ALIGN_BOTTOM_LEFT,
        'size' => PopoverX::SIZE_MEDIUM
    ]
]);

yii2-ipinfo is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.