How to block an IP address in Shopify

Maintaining a secure and reliable store may involve taking measures such as blocking specific IP addresses. Learn here how to block an IP address with code
Froonze
June 21, 2024

Keeping your Shopify store secure is paramount. While Shopify itself offers robust security features, there might be situations where you need to take additional steps, such as blocking specific IP addresses.  Perhaps you've identified a source of malicious activity or persistent spam comments.  

This article will explore two main methods for blocking IP addresses in your Shopify store: using a third-party app and implementing a custom code solution.

How to Block an IP Address in Shopify

Blocking IP Addresses with Shopify Apps

The Shopify App Store offers a variety of apps designed to enhance your store's security and functionality.  Many of these apps include features for blocking IP addresses.  Here's why using an app might be the best option for you:

  • User-friendliness:  Most apps are designed with a user-friendly interface, making them easy to set up and manage even for those with limited technical knowledge.
  • Customization Options:  Apps often offer various customization options, allowing you to specify the duration of the block, create blocklists, and potentially whitelist specific IP addresses.
  • Additional Features:  Many security apps offer additional features beyond IP blocking, such as fraud detection and spam filtering, providing a comprehensive security solution for your store.

Finding the Right App:

Head over to the Shopify App Store and search for "IP Blocker" or "Security", or directly on the Fraud category. Several reputable apps offer IP blocking functionality. Carefully review app descriptions, pricing plans, and user reviews before making your choice. We'd recommend you starting with the free Fraud Filter, an app developed by Shopify itself to strengthen your defenses against fraud. Its easy-to-use rules let you set up warnings or block suspicious orders entirely, preventing chargebacks.

Custom Code Solution for Blocking IP Addresses (Advanced Users)

For users comfortable with coding, a custom code solution offers a more granular level of control over IP blocking. However, this approach requires some technical expertise. It uses an external IP service to detect visitors' browser IPs and replace or lock content, discouraging continued traffic from targeted IPs. IPInfo offers a free plan for up to 50k requests per month so we have used this service in the code below. While you might need to purchase a plan if you exceed a high number of requests per month (which is unlikely if targeting a single IP):

  1. Identify the IP address you want to block (if you have an order from this customer, you can identify the IP address in the order's fraud analysis)
  2. Go to your Shopify admin and navigate to Online Store > Themes. Find the theme you want to edit and click on the Three points icon > Edit code.
  3. In the Theme Editor, search for theme.liquid. Click and open it.
  4. Inside, find the end of the body: </body>
  5. One line above, paste the script below. You would need to replace the Target IP with the IP you aim to block. An IP is formatted in a set of four numbers separated by dots (e.g 192.158.1.38)
  6. Save and preferably, test that it's working on site. For example, you can try blocking your own IP first, to check that it works.

<script> document.addEventListener('DOMContentLoaded', function() { fetch('https://ipinfo.io?callback') .then(res => res.json()) .then(json => { if (json.ip == "yourtargetip") { document.querySelector('main').innerHTML = `<div class="content_blocked" style="padding: 10rem; text-align: center; margin: auto">This content is not available in your country</div>`; } }) },false); </script>

Block IP address Shopify

Conclusion

Blocking IP addresses can be a valuable tool for maintaining a secure and reliable Shopify store.  Whether you choose a user-friendly app or opt for a custom code solution (for advanced users), there are options available to fit your needs and technical expertise.  By taking these steps, you can create a safer and more secure shopping environment for your customers.

You may want to have a look at other of our articles regarding this subject, so here are the links:

Froonze Customer Accounts Concierge
Get the app today
Turn the account page into a beautiful portal with
Froonze Customer Accounts Concierge
  • 220+