Accessing Environment Variables on an Azure Web App in PHP

August 16, 2015 by in category Azure, Microsoft, PHP, Web Development tagged as , , , , with 0 and 3
Home > Blog > Microsoft > Azure > Accessing Environment Variables on an Azure Web App in PHP

TLDR; If you want to access environment variables set on your Azure Web App in PHP, usegetenv(). Example below:

$api_key = getenv('ENVVARNAME');

While working on the contact form for my personal website, I ran into issues with accessing environment variables I had set in my Azure Web App.

When building my contact form I decided to use SendGrid to power it (which I purchased from the Azure Marketplace). In order to use the SendGrid API I needed to include the API keys and login credentials. It’s very bad practice to include these keys in your source code (especially when open source like my website), so I added some environment variables in the configuration section of my Azure Web App.

Azure App Settings

Since these variables are set for the whole app, I figured they were global. So I tried accessing them like so: $_ENV["SGUSER"]. However, after much testing I found that accessing them with $_ENV would yield nothing. I tested with $_SERVER as well with the same result. So, after a bit of searching on Bing I found the getenv() function. This seemed to do the trick!

Add comment

Made with ♥ in Houston