How to Get and Set PHP Custom Environment Variables

Apache Get Set Environment Variables

like windows Environment Variables we have some variables at Apache/PHP level, there are some default Environment Variables while we can also set our custom variables according to need of our application.
Scope wise these are Super Global Variables, although we can limit our custom vars to our application only we have discussed it step 4 below.
Lets have a look at PHP Default Environment Variables, we can view all Environment Variables executing the following code.

it will give us the following array of different variables
Default Environment Variables

we can access single variable by using $_SERVER array or getenv() function as below

Setting Custom PHP Environment Variables

Yes we can store our custom variables too, and that can be accessed and used in the same manner as default Environment Variables.

Why We Need to User Custom Environment Variables

In short we need to use Custom Environment Variables to make our application more secure.
While developing dynamic websites we connect to database, email systems and other external components or services through their credentials like Host, User, Password, Port etc.
It is bad practice to write these credentials inside our PHP coding because if somehow someone got access to our code he/she can gain access to our database, email or other connected service and can make unwanted changes or steel our sensitive information.
To avoid it we have an option to keep these credentials as variables outside PHP code in some secure directory of our server, Environment variables gives us this facility.

How to Set Custom Environment Variables

There are several ways to set Custom Environment Variables few of them are given below.

  1. Using putenv() function
  2. using .htaccess file
  3. using Apache config files (httpd.conf)
  4. using virtual hosts (httpd-vhosts.conf)

1- Set Environment Variables Using putenv() function

This method sets Environment Variable temporarily, once script is ended, Variable(s) set by this method will no longer exist. it can be set and accessed as the following code snippet.

Note: As its values is written inside PHP code so it is not recommended to use for security purpose.

2- Set Environment Variables Using .htaccess file

If you project has .htaccess file write the following code at end of that file, else create a file named .htaccess and write the following code.

Above 4 variables will be set instantly and we can use it in our PHP file for database connection as below

see above implementation the values of all variables are outside PHP coding, it is bit secure than writing these credentials in PHP as below

3- Set Environment Variables Using Apache config files (httpd.conf)

To make it more secure we can write it in Apache config files like goto C:\wamp\bin\apache\apache2.2.23\conf and open httpd.conf file in notepad or some other editor and write the following code at end

As we have placed our code at Apache level, so we will have to restart Apache before accessing these variables because it will not be set before restart of Apache.
we can access these variables same as we did in above example

Note:This is most preferred way to Set Custom Environment Variables in a sense of security.

4- Set Environment Variables using virtual hosts (httpd-vhosts.conf)

I have also written a detailed blog on Virtual Hosts
If you have setup custom virtual host for your website you can write environment variables in that file too
which will also need to restart Apache and these variables will only be accessed in that project.
virtual hosts files can be seen at the following path
open file named httpd-vhosts.conf if you have setup custom virtual hosts it will have few entries like the following

you can write Custom Environment Variable before ending VirtualHost tag as below

It will be accessible after restarting Apache, using same method as given below

This method has extra feature of hiding Environment Variables from other virtual hosts (domain names), Means this method lets us set domain specific Custom Environment Variables.

It depends on you server and project architecture, choose your suitable method.
Method no 3 which Set Environment Variables Using Apache config files (httpd.conf) is recommended and mostly used.

hope this helped you out, let me know if you have any issue or want to share something that I have missed in this topic.


    Write a Reply or Comment

    Your email address will not be published. Required fields are marked *