Search code examples
javascriptip-addresswhitelist

How do I White-list IPs With Javascript?


Is there any way to white-list an IP Address using solely JavaScript or do I have to use php or some other server-side language? By white-list, I mean only a certain number of specified IP's can access a webpage. If this isn't possible with JavaScript, then please let me know.


Solution

  • It is possible but would be very easy to circumvent client-side IP checking. All a user would need to do is disable Javascript in their browser and they'd be able to access the site.

    IP whitelisting is typically something that's done by the web server (Apache, Nginx, IIS); Not web applications (PHP, Python, NodeJS) and especially note client-side scripts.

    If you really want to do this client-side, you have to actually call out to a service that tells you what IP address you're using. See this SO question for examples of how to do this. You can then compare the IP address against an array of allowed IPs. If it doesn't match any, the easiest way to deny access is to redirect the user away from the site.

    Once again, CLIENT-SIDE WHITELISTING IS INSECURE AND NOT AT ALL RECOMMENDED!