Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I modify the path used by exec in php

Tags:

php

apache2

There are some scripts that need certain paths to be set in environment to run. I want to edit my path to include those locations. These are the locations I want to add to my exec path.

 $JAVA_HOME = "/usr/java/jdk1.6.0_31";
 $ANT_HOME = "/usr/apache-ant-1.8.3";
 $ANT_BIN = "$ANT_HOME/bin";
 $JAVA_BIN = "$JAVA_HOME/bin";
 $ADDPATH=$JAVA_HOME . ":" . $ANT_HOME . ":" . $ANT_BIN .":" . $JAVA_BIN . ":" . $PATH;

And i used putenv

 putenv("JAVA_HOME=" . $JAVA_HOME);
 putenv("ANT_HOME=" . $ANT_HOME);
 putenv("ANT_BIN=" . $ANT_BIN);
 putenv("JAVA_BIN=" . $JAVA_BIN);
 putenv("PATH=".$_ENV["PATH"].":".$ADDPATH);

However when I do a

echo getenv("PATH");

i get

/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

I have two queries : (a) How do I set the path variable to the values I want (b) Is there a way ( or a location ) to add these locations to be a part of server environment ( within php.ini or apache configs ) rather than use the script to make these edits.

like image 411
Ravi Nankani Avatar asked May 26 '12 06:05

Ravi Nankani


People also ask

What is PHP exec command?

The exec() function is an inbuilt function in PHP which is used to execute an external program and returns the last line of the output. It also returns NULL if no command run properly.

Is it safe to use exec in PHP?

The exec function is as safe as you make it. As long as you use the proper escaping functions like shown here, you'll be good. While it is possible to make the script commands safe, a common attack vector is to upload a malicious script and use exec and similar functions to hack the server.

How do you check if exec is enabled in PHP?

php phpinfo(); ?> You can search for disable_functions and if exec is listed it means it is disabled. To enable it just remove the exec from the line and then you need to restart Apache and you will be good to go. If exec is not listed in the disable_functions line it means that it is enabled.


1 Answers

Try to use apache_setenv() and apache_getenv().

like image 161
flowfree Avatar answered Sep 21 '22 04:09

flowfree