PHP AdBlock Detection script.

This small library attempts to inform PHP that the current visitor has AdBlock enabled. It does this by:

  1. Detecting AdBlock on the client side (through JavaScript and JQuery).
  2. Sending off an AJAX request to the server so that PHP can keep track of the user’s AdBlock status (enabled or disabled) via a session variable.

Note: PHP cannot detect AdBlock by itself, simply because AdBlock does not modify the HTTP request headers that are sent by the browser. Although this method isn’t infallible, it is probably the closest that you are going to get to detecting AdBlock on the server side.


1. What is meant by “AdBlock status”?

When I say “AdBlock status”, I’m talking about whether the user has AdBlock enabled or disabled. i.e. Is it on or off. This script will only detect users that have it enabled for YOUR website.

2. How does the detection script work?

A visitor lands on your website for the first time. As soon as the page is loaded, our JavaScript library attempts to figure out if he or she has AdBlock enabled. If AdBlock is enabled, an AJAX request is sent off to a PHP script, which stores the user’s AdBlock status in a session variable. The page is then refreshed via JavaScript. You can then use this session variable in whatever way you see fit.

3. Will this library work 100% of the time?

I cannot promise that this method of detection will always work, simply because the user has control over his or her browser. Ultimately, anything that is sent to the client can be modified or deleted. Fortunately, most Internet users do not have the technical expertise to do this.

4. What can this be used for?

You could use this library for any number of reasons:

  1. Tracking AdBlock usage statistics: You could log usage to a database for analytical purposes.
  2. Blocking AdBlock users: You could block AdBlock users from accessing certain content.
  3. Remind users of the extension that your website relies on advertising revenues.
  4. Find out what forum users have the extension enabled.

5. Can I see an example of the library being used?

Sure thing! Here is a file called example.php, which I have included in the ZIP file:

Some things to note:

  1. Sessions must be started before you attempt to include or use the QuitHogging.php file! This is because the library uses session variables to keep track of AdBlock users.
  2. The QuitHogging.php file will automatically create a named constant called ADBLOCK_ENABLED. This constant will be TRUE or FALSE, thereby allowing you to use it in IF statements (you can see an example of this in the PHP script above).
  3. This library requires JQuery!
  4. The qh_JS() function will include the necessary JS files and JS variables. Place this just before the ending body tag.

6. What happens if the user whitelists my site?

An AJAX request is sent whenever the JS script notices that the user’s AdBlock status has changed. If the user disables the extension or whitelists your website, the page will refresh and the ADBLOCK_ENABLED variable will be FALSE.

7. Redirecting the user if AdBlock is enabled.

If you’re wanting to redirect users, you can use the function qh_redirectIfAdblockEnabled, which can be used like so:

Having issues? Email and I’ll get back to you as soon as possible!