When setting env variables, how to get them using PHP?

Posted November 6, 2015 13.7k views

I’m using this command to set environmental variable
export COMMIT_ID='x'

When I use printenv I can see it defined. However, PHP can’t see it using getenv('COMMIT_ID')… What am I doing wrong?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

Hi there!

What is probably happening is that you set the environment variable with a regular system user (the one you are logged in), but as the web server runs with another user (usually www-data), you don’t have access to that variable from inside your PHP code.

If you are using Ubuntu, you might try setting the variable in the file /etc/environment, so it will be available to all users in the system. This might help:

you can set all your environmental variables in /etc/environment and access them from php using getenv function