Load snippet based on domain name


#1

I am just wondering if there is a more concise / smarter way of doing this. I want a snippet to only load if the domain is the live domain. I have three variables in my config with live, staging and local set. So if the domain contains either staging or local, dont load the snippet. (it contains analytics tracking codes, so dont want false hits).

It seems to work with this:

<?php
    $url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
    if (strpos($url,c::get('staging')) !== false || strpos($url,c::get('local')) !== false ) {
      // Local or staging
    } else {
      // Live
      snippet('ga-header');
    }
?>

I just cant help thinking theres a better or more accurate way…


#2

Why don’t you use a config.php setting (with a config.php for each server)

c::set('analytics', true); // or false for local and staging

And then just check this setting.


#3

Thats so brilliantly simple its made me feel brilliantly stupid for not thinking of it. Thanks :slight_smile: