Code Snippet: Display content filtering Public IP address

Someone got an approved Google AdSense account, and they started displaying ads onto their site. But on the third day, they were banned for invalid clicks. The reason they guessed was, multiple clicks from their own public IP can ban them –  though they are not so sure. It can happen while multiple user in multiple computers using a same internet connection. When they are clicking google tracked them as the same person who registered the AdSense account.

Okay, this can be one scenario, but you may face situation like this, when you may need to block certain things to certain IP addresses. Here’s the code snippet (charm! 🙂 ) that can save you.

There are many versions of tracking IP addresses using PHP, I found this one working for Public IPs – most of the time (sometimes it missed too). And we’ve created a function to check whether the mentioned IP address/addresses is/are detected or not. If any of the blocked IP address detected, we’re returning false, and vice versa.

And we can use this function like this:

WordPress usage

  • For theme: you can place the two functions in functions.php and can use them anywhere within the theme.
  • For plugin: you can place the two functions anywhere within your plugin file and can use them all over the plugin, and even in theme.

Mayeenul Islam
Front End Designer & WordPress Developer

Comment here...