How to locate and identify website visitors by IP address in Laravel 6 and PHP
If you need to know exactly who your visitors are and where they’re coming from, you can use ipstack to identify websites visitors. The great thing is, this service is free to use!
What is ipstack?
ipstack is a good service that offers one of the leading IP to geolocation APIs and global IP database services worldwide. ipstack real-time geolocation API service is used by thousands of developers, SMBs and large corporations all around the world, including some popular names, such as Microsoft, Samsung, Airbnb, etc.
Know your visitors
Know where your customers access your website from and customize user experiences based on IP.
IPv4 and IPv6 Data
The API covers over 2 million unique locations in more than 200.000 cities around the world.
Years of partnership with large ISPs ensure accurate and consistent data any day of the year.
Easy to scale
The ipstack API was built with scalability and ease of use in mind. Implementation takes less than 10 minutes and uptime is guaranteed.
Powerful & Scalable
From 100 requests a month to a million requests a day.
An extensive API documentation and a quickstart guide will get you up and running within minutes.
All data sent to and processed by the ipstack API is secured via 256-bit SSL encryption (HTTPS).
JSON or XML
ipstack API can deliver results in JSON or XML format.
You can read the official documentation on the ipstack website.
How to use
Register to get a free API key
You need to go to ipstack and register to get a free API key.
Using Raw PHP
You can use raw PHP to identify user IP. Here is the sample code (which can be found at ipstack documentation):
Standard Lookup: Find below an example for the Standard Lookup Endpoint using PHP cURL.
// set IP address and API access key $ip = '184.108.40.206'; $access_key = 'YOUR_ACCESS_KEY'; // Initialize CURL: $ch = curl_init('https://api.ipstack.com/'.$ip.'?access_key='.$access_key.''); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Store the data: $json = curl_exec($ch); curl_close($ch); // Decode JSON response: $api_result = json_decode($json, true); // Output the "capital" object inside "location" echo $api_result['location']['capital'];
Using Laravel package
Here is a great Laravel package which we can use to easily check the IP:
First, install via composer
composer require pulkitjalan/geoip
There is a Laravel service provider and facade available.
Add the following to the providers array in your config/app.php
Next add the following to the aliases array in your config/app.php
'GeoIP' => PulkitJalan\GeoIP\Facades\GeoIP::class
php artisan vendor:publish --provider="PulkitJalan\GeoIP\GeoIPServiceProvider" --tag="config" to publish the config file.
After that, to use the ipstack as the driver set the config:
Example: $config = [ 'driver' => 'ipstack', 'ipstack' => [ 'key' => 'YOUR IPSTACK KEY', ], ];
Finally, you can use ipstack like this:
<?php use PulkitJalan\GeoIP\GeoIP $geoip = new GeoIP(); $lat = $geoip->getLatitude(); // 51.5141 $lon = $geoip->getLongitude(); // -3.1969
The geoip class takes a config array as the first parameter or defaults to use the ip-api driver.
Source Code of the package:
If this post was helpful, please share this post to your friends and don't forget to follow our Facebook and Twitter pages!
This post is submitted by our members. Submit a new post.
Tags: Tutorials Laravel 5.8 Laravel 5.7 Laravel 5.6 Laravel 5.5 Laravel 5.4 Laravel 5.3 Laravel 5 Laravel 5.1 Laravel 5.2 Intermediate Laravel 6