Trying to build some customisation into your website? Or simply block some malicious IPs from accessing your content?
We will cover 2 methods:
Important: This method is now no longer supported by several browsers.
Continue the reading below to see another free alternative.
This method uses diafygi "STUN IP Address requests for WebRTC".
This code snippet is based on newly-released WebRTC support in Chrome and Firefox that allows request for STUN servers to be made, resulting in the ability to access local and public IP addresses for your visitors.
The good news is that these calls can't be blocked by ad blocking plugins and widgets because they are made outside of the XMLHttpRequest instance. Given that 20 to 30% of the internet users are browsing with Adblockers, this is perfect if you want to ensure aconsistent experience for all your users.
Copy and paste the following code into your web page:
Now you can use the following function to get the IP as soon as it's found:
Abstract provides a free IP detection API that has a method to retrieve the IP of a visitor.
This simple to use, yet powerful API provides the following features:
The API is free to use and allow thousands of calls per month.
Using it is pretty simple and only require an API key you can get for free by signing up here.
The documentation can be found on the documentation page: ip geolocation documentation