How to Get and Set PHP Custom 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.
1 2 3 |
echo '<pre>'; print_r($_SERVER); echo '</pre>'; |
it will give us the following array of different variables
we can access single variable by using $_SERVER array or getenv() function as below
1 2 3 4 5 6 7 |
echo $_SERVER['HTTP_HOST'] . '<br>'; echo $_SERVER['REMOTE_ADDR'] . '<br>'; echo $_SERVER['SCRIPT_FILENAME'] . '<br>'; //or we can also get above 3 variables by echo getenv('HTTP_HOST') . '<br>'; echo getenv('REMOTE_ADDR') . '<br>'; echo getenv('SCRIPT_FILENAME') . '<br>'; |
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.
- Using putenv() function
- using .htaccess file
- using Apache config files (httpd.conf)
- 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.
1 2 3 4 5 6 |
//Set Environment Variable //with name=var1 and value=secure_value putenv("var1=secure_value"); //to access it getenv("var1"); |
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.
1 2 3 4 |
SetEnv host localhost SetEnv user root SetEnv password test SetEnv database my_project |
Above 4 variables will be set instantly and we can use it in our PHP file for database connection as below
1 2 3 4 5 6 |
$db= new mysqli($_SERVER['host'],$_SERVER['user'],$_SERVER['password'],$_SERVER['database']); if($db->connect_error) { echo "ERROR: (".$db->connect_errno.") ".$db->connect_error; exit(); } |
see above implementation the values of all variables are outside PHP coding, it is bit secure than writing these credentials in PHP as below
1 2 3 4 5 6 7 8 9 10 |
$db_host = "localhost"; $db_user = "root"; $dp_pwd = "test"; $db_name = "my_project"; $db= new mysqli($db_host,$db_user,$db_pwd,$db_name); if($db->connect_error) { echo "ERROR: (".$db->connect_errno.") ".$db->connect_error; exit(); } |
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
1 2 3 4 |
SetEnv host localhost SetEnv user root SetEnv password test SetEnv database my_project |
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
1 2 3 4 5 6 |
$db= new mysqli($_SERVER['host'],$_SERVER['user'],$_SERVER['password'],$_SERVER['database']); if($db->connect_error) { echo "ERROR: (".$db->connect_errno.") ".$db->connect_error; exit(); } |
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
C:\wamp\bin\apache\apache2.2.23\conf\extra
open file named httpd-vhosts.conf if you have setup custom virtual hosts it will have few entries like the following
1 2 3 4 5 |
<VirtualHost *:80> ServerName my_project.com DocumentRoot c:/wamp/www/my_project </VirtualHost> |
you can write Custom Environment Variable before ending VirtualHost tag as below
1 2 3 4 5 6 7 8 9 |
<VirtualHost *:80> ServerName my_project.com DocumentRoot c:/wamp/www/my_project SetEnv host localhost SetEnv user root SetEnv password test SetEnv database my_project </VirtualHost> |
It will be accessible after restarting Apache, using same method as given below
1 2 3 4 5 6 |
$db= new mysqli($_SERVER['host'],$_SERVER['user'],$_SERVER['password'],$_SERVER['database']); if($db->connect_error) { echo "ERROR: (".$db->connect_errno.") ".$db->connect_error; exit(); } |
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.
Comments