简体   繁体   中英

PHP - exec,shell_exec,system not working

i read so many similar questions but nothing works with me

im using wamp 2.2 ,Apache 2.0 , PHP V5.3.8

safe_mode=off - disabled_functions deleted from php.ini i'm trying to exec

exec("chrome.exe google.com");

nothing happens and when i try

exec("calc");

the windows shows weird message to execute calc in different user although i changed the user for the apache service to Administrator and i verify the user using

exec("whoami");

where is the problem?

Environment path for CMD and php shell might not be the same. You either have to give entire path of the file, or export the path of chrome.exe in php first

Also, I believe that chrome.exe opening a page requires XServer, php can't open graphical interfaces in shell. These commands are not passed to actual shell as a parent, so php file can't "launch" applications for you.

Please read specifics of the exec function: PHP exec Reference

Also try adding 2> errors.txt to see what are the errors if any in the execution of the programme.

--

The program needs to know what X server to connect to, and it needs to have permissions to connect to that server. You specify the X server with the DISPLAY environment variable; this will usually be set automatically if you are running the PHP program from a terminal in X, or from a GNOME panel or something similar; however, if you are running the PHP script in some other manner, it likely won't know what X server to connect to.

X has various ways of specifying permission to connect to a server, but the most common one is using a file called ".Xauthority" in the users home directory. Because only the user who is logged in at the X server can read this file, they are the only user who can run GUI programs. So, if you start the PHP user as the same user who is logged in at the X server, you shouldn't have any problem with permissions. However, if the PHP program is running as a different user, you will have to give that user permission to access the X server.

Reference: http://bytes.com/topic/php/answers/838364-cant-launch-graphical-apps-php-exec-ubuntu-8-04-system (I know that link is for linux and won't have exact same solution for Windows, but exec() still needs to know which X interface to refer to)

Stop Apache running as a service.

When windows runs a service it it is not running directly as the user that started the server or manages due to this it could well be starting chrome in a service environment so you wont see it load on your desktop there are 2 ways to get around this

Stop apache service browse to your apache directory and run httpd.exe manualy then try your script it should work or if it is not required to be running though a web request so it not using anything from the browser you can allow it to work with c:\\wamp\\php\\php.exe yourfile.php (your php path should be replaced for c:\\wamp\\php )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM