Skip to main content

Cannot retrieve environment variables with getenv()

Comments

6 comments

  • cPRex Jurassic Moderator
    Hey there! This isn't really a cPanel issue, but I think we can get this working for you! You need to declare the variable first - try something like this in the PHP file:
    That should print out "example" if things are working as they should in the .bashrc
    0
  • Lauris
    Hey there! This isn't really a cPanel issue, but I think we can get this working for you! You need to declare the variable first - try something like this in the PHP file:
    That should print out "example" if things are working as they should in the .bashrc

    It does not work. Empty output.
    0
  • cPRex Jurassic Moderator
    In my test, I have the following in the .bashrc file: export RANDOM_VALUE="stuff"
    and then this code in the phptest.php file:
    With both of those things in place I am able to execute the script: $ php phptest.php stuff
    If that isn't working, there is likely some misconfiguration in your environment.
    0
  • Lauris
    From CLI it is working as it should, but if I execute that php fine in browser it does not show nothing. Do I need Apache module for that? I am using latest cPanel version and php 7.3 and 8.0
    0
  • cPRex Jurassic Moderator
    This type of programming is a bit beyond the scope of cPanel Support, as you end up getting into environment variables and whether or not they are global, local only to BASH, local to only that session, and many other things. As I am not a PHP developer, I certainly don't claim to be all-knowing in how that gets handled. There are also ways to have PHP call a BASH script, such as what is outlined here:
    0
  • Lauris
    I got it working by registering my App in cPanel > Application Manager and defined env. variables there. Not sure if this is correct way to define environment variables per hostnames, but at least it looks quite easy to set up.
    0

Please sign in to leave a comment.