You still have not answered my question. You have defined 2 scripts. The first one at the top of the post. Is the one you are you running from the *nix prompt? If so, you are running it with the user and environment of your shell prompt. In the apache server, the script runs as the apache user (which I believe you can set somewhere in the apache configuration). I am not sure, but you might be able to set the environment for when scripts are run from within the apache config as well.