SMF Using Nginx to enable special variables

I came up with this little trick while working with my SMF installs. I wanted to be able to present $db_show_debug to myself but not others. There are many ideas involved in this including waiting until we loaded enough of the user profile to check if they are a admin and then enabling it.

However I wanted to go further, I wanted to do this at a very low level, at the very startup of SMF.

Luckily Nginx has a way to help us. Apache may have this, but I haven’t researched it. This requires for nginx the GEO module.

At the top of my server configuration I added

geo $smf_admins {
  default 0;
  127.0.0.1/32 1;
  8.8.8.8/32 1;
}

You should fill this out using proper IPs and CIDR notations. For those with dynamic IPs, this won’t work.

Now later on in my php block, I defined to add a server attribute

location ~ \.php$
{
                include         fastcgi_params;
                fastcgi_param   smf_admins $smf_admins;

Now finally, in my Settings.php, I can add a check for this new $_SERVER variable.

$db_show_debug = !empty($_SERVER['smf_admins']) ? true : false;

Now automatically for me, I am presented with debug mode in SMF without the whole world seeing this.

For those with dynamic IPs, I haven’t tested this, but nginx allows you to retrieve cookies by $cookie_COOKIENAME. So you could add a map to find it. You would need a way to add the cookie and it isn’t entirely secure as somebody could find your cookie name.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.