I want to access my environment variables into a php file through the web user.
Not when i execute printenv
on the server, that particular environment variable gets displayed,
but on running it as a web user, sudo -u www-data printenv
it is not displayed.
My server is an apache server hosted on Ubuntu 14.04 on DigitalOcean, any help would be much appreciated.
Thanks

- 1,411
- 16
- 37
2 Answers
-
It isn't working. I'm getting a permission denied message. `{"errors":[{"message":"Permission denied, wrong credentials","field":null,"help":null}]}` – harshithdwivedi Aug 07 '16 at 22:18
-
Really? If you're using `$_ENV`? I guess you get this error if you're using `sudo -u www-data printenv`. Why do you want to run it anyway? Executing `printenv` should give you the same results when you run it via PHP because the web server should run with **www-data** user rights! – csabinho Aug 07 '16 at 22:34
-
No, running the php script as www-data gave me this error. I want to run it as www-data because this php script is to be called when the user presses a button on server hosted webpage. – harshithdwivedi Aug 07 '16 at 23:11
First, check you can get the data you want "manually", on your shell :
sudo su yourusername -c printenv
Is this the data you are looking for ? You were asked a password, right (should be in most case)?
For www-data can acces your environnement variable, it should log "as you" for check, so it ask for a password and fail, as we don't passed it through php. And we won't.
The proper way for doing this, without security issue, is to use sudoers, wich allow some user to do some commands without being asked a pass. To use it, run the command visudo as root, then add this at the end of the file
www-data ALL = NOPASSWD: su yourusername -c printenv
Now the provious command can be runned from the user www-data without pass, so PHP can use it.
Edit: this way you can't access user environnement without knowing their username, but it's fine for your own use.

- 1,192
- 1
- 12
- 22