Tag Archives: php

Babylonic Apache SetEnv

The Mission

The other day I had a PHP-project based on the ZendFramework where I had to display different logos whether the project was viewd on the preview or on the live system.

Until now I always exchanged the images after puling the live data from the repository. Of course this was one step that sometimes was forgotten, sometimes the correct images where already in the repository so ther was no need to do it – -all in all it was rather anoying. Yes, you could say: “Automate processes that are always the same” but that is not the point today.

I had something different in mind.

For the ZendFramework I already set an environment variable “APPLICATION_ENV” that defined whether the server hosts a development or a production environment.

Why not use that information to display a logo according to the environment?

So I had either the option to call the logo via a PHP-Script that checks the environment variable and returns the correct image.

Or I could use the – already for ZendFramework available – mod_rewrite to do some rewriting voodoo.

The first option would have meant to write a complete PHP-Script and call that every time the logo is asked for. Somewhat much to do for a simple rewriting I thought.

The Quest

So I went for the second option. And it took me some time to get it up and running.

What happened: I took the documentation for mod_rewrite and went through it. I already had the following section in my VirtualHost-Config

SetEnv APPLICATION_ENV development

So for the directory with the logos in I created the following .htaccess-file (Yes, I could also put it into the servers config file…)

RewriteEngine on
RewriteCond %{ENV:APPLICATION_ENV} development [NC]
RewriteRule ^logo(?!-preview)(.*)\.png$   logo-preview$1.png [NC]

Fine I thought and tried that!

No Luck.

Didn’t work

Not after hours of debugging, reading blogposts and forum-entries and rewriting the code one or the other way.

No Luck at all

During these hours of searching the net I found that I wasn’t the only one having that problem, but mostly the solution was to set the environment variable in the Rewrite Engine as well as via SetEnv. But there had to be a different way.

And there was.

The Happy End

After hours I finaly found http://httpd.apache.org/docs/2.0/env.html and there was a paragraph headed “Some Caveats”.
Bingo! Thats It.

SetEnv is called after the mod_rewrite-calls. SetEnvIf before!

So thats the solution. Use SetEnvIf instead of SetEnv.

So I changed my VirtualHost-Config the following way

-  SetEnv APPPLICATION_ENV development
+  SetEnfIf HTTP "HTTP.*" APPLICATION_ENV=development

Yes It takes some performance, because APPLICATION_ENV is now set on every Request, but as long as static Environment variables are set after dynamic ones, that is an SEP to me.

The ZendFramework-Projects behavior does not change as the change is completely transparent to it. But reloading the log now suddenly showed the correct result!

The Polish

Well I thought, fine. But why not set the APPLICATION_ENV according to the called host-name? And therefore moving the SetEnfIf-call from the virtualServer to the projects htaccess-file? At least that’s where I think it belongs to, as it’s a project-specific setting.

So I removed the SetEnvIf-Line from the VirtualHost-Config and placed the following into the projects .htaccess-file.

<IfModule setenvif_module>
    SetEnvIf Host "\.?(preview|stage|staging|development|dev|local)\.?" APPLICATION_ENV=development
</IfModule>

As soon as one of preview, stage, staging, dev, development or local is a part of the hostname the APPLICATION_ENV-variable will be set to “development”.

One could now change that to the local environment by adding other names as well, but for me it is sufficient.

Barcamp Darmstadt

This weekend I will be attending my first BarCamp ever!

On the 20th and 21th of November Darmstadt hosts the second BarCamp at the Rhine-Main-Region. And I will be one of the about 300 people attending.

And to make that BarCamp more interesting to the PHP-Community, we try to coordinate a complete track on PHP-related sessions. Being a novice to the BarCamp I want to give a session and choose to introduce Zend_Form as it is one of the components of the ZendFramework that makes my life as Developer easier every day.

So let’s see what the weekend brings!

Social and Ethical Responsibilities

This morning I was attending Zeev Suraskis keynote at the International PHPConference in Mainz/Germany.

He spoke not so much about PHP or new technologies but what happens when technology fails. He gave us an insight into the things that happened around his last holiday in the south of Italy starting with a lost Internet-connection through a malfunctioning lens, a dead GPS-Display and his troubles getting mobile internet-access up to the dead fridge back home.

Besides the strange accumulation of bad luck it again showed me how much we depend on technology.
Continue reading Social and Ethical Responsibilities

Valid Syntax errors: Part 1

It took me about an hour to spot a strange behaviour in my PHP skript:

Have a look at this code:

$foo = array ( 'a', 'b', 'c' );
foreach ( $foo as $bar );
    echo $bar . "\n";
// Expected Output:
// a
// b
// c
// Actual Output: 
// c

What happened?

Would I have used phpcs it would have been obvious. I used a semicolon instead of a bracket after the foreach statement.

So instead of calling the echo command with each iteration the foreach silently iterated through $foo and assigned $bar on each iteration. That left $bar with the last value of the array that gave the strange output.

I can not actually remember whether there had been closing brackets but I did not get an Error regarding that.

phpcs would have detected the missing brackets — if that would be observed by the used sniff.

More on phpcs later.